当前位置: 首页 > wzjs >正文

未备案网站网站运营服务商

未备案网站,网站运营服务商,有什么做网站的国企,做网站入门看什么书📚 一、IO流初探:数据的魔法传送门 🔮 💡 流是什么? 在Java世界里,IO流就像是一个神奇的数据搬运工🚚,它能把数据从一个地方传送到另一个地方。 就像哈利波特里的“呼噜网”一样&am…

📚 一、IO流初探:数据的魔法传送门 🔮


💡 流是什么?
在Java世界里,IO流就像是一个神奇的数据搬运工🚚,它能把数据从一个地方传送到另一个地方。

就像哈利波特里的“呼噜网”一样,可以把人从一个壁炉传送到另一个壁炉🔥。

🧭 分类地图 🗺️

          ┌───────────────┐│    抽象基类    │└───────────────┘▲┌────────┴────────┐│                 │字节流InputStream    OutputStream ⚡(适合二进制文件)│                 │字符流Reader        Writer 📝(适合文本文件)


💾 二、字节流篇:最原始的力量 ⚡


1. FileInputStream & FileOutputStream
像两个勤劳的小精灵🧚,一个负责把文件内容一点点搬出来,一个负责把内容放回去📦。

示例代码:

try (FileInputStream fis = new FileInputStream("input.txt");FileOutputStream fos = new FileOutputStream("output.txt")) {int data;while ((data = fis.read()) != -1) {  // 👀 读取一个字节fos.write(data);  // ✍️ 写入一个字节}
} catch (IOException e) {System.err.println("Oops! 出错了!💥" + e.getMessage());
}


2. BufferedInputStream & BufferedOutputStream
带背包的小精灵🎒,一次可以搬运更多数据,效率提升🚀!

示例代码:

try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream("input.txt"));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("output.txt"))) {byte[] buffer = new byte[1024];  // 背包容量1024字节🎒int bytesRead;while ((bytesRead = bis.read(buffer)) != -1) {bos.write(buffer, 0, bytesRead);  // 搬运整个背包内容📦}
} catch (IOException e) {System.out.println("传输失败!⚠️ " + e.getMessage());
}


📝 三、字符流篇:专为文字设计的搬运术 📖


FileReader & FileWriter
它们懂中文、英文、emoji 😄,能处理各种语言的文本。

示例代码:

try (FileReader reader = new FileReader("input.txt");FileWriter writer = new FileWriter("output.txt")) {int charCode;while ((charCode = reader.read()) != -1) {writer.write(charCode);  // 写入字符✍️}
} catch (IOException e) {System.out.println("哎呀,写崩了!😵‍💫");
}


BufferedReader & BufferedWriter
超级高效的图书馆管理员📚,可以一次读一行!

示例代码:

try (BufferedReader br = new BufferedReader(new FileReader("input.txt"));BufferedWriter bw = new BufferedWriter(new FileWriter("output.txt"))) {String line;while ((line = br.readLine()) != null) {bw.write(line);bw.newLine();  // 换行✅}
} catch (IOException e) {System.out.println("读取失败,是不是书太厚了?📖💔");
}


🧠 四、对象流篇:让对象飞起来!✈️

ObjectOutputStream & ObjectInputStream
可以直接把对象打包发送📦,还能还原回来🔄!

示例代码:

// 写入对象
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("person.ser"))) {Person person = new Person("张三", 25);oos.writeObject(person);  // 发射对象导弹🚀
} catch (IOException e) {System.out.println("对象发射失败!💣");
}// 读取对象
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("person.ser"))) {Person person = (Person) ois.readObject();System.out.println("接收到的对象:" + person);
} catch (IOException | ClassNotFoundException e) {System.out.println("对象接收失败,是不是掉沟里了?🕳️");
}


📌 注意事项:

类必须实现 Serializable 接口🧾
可以用 transient 标记不想被保存的字段🚫
建议手动定义 serialVersionUID🔒
🔁 五、转换流:字节与字符之间的翻译官 🧑‍⚖️
InputStreamReader & OutputStreamWriter
相当于中英翻译器🌍,让不同语言之间沟通无障碍🗣️

示例代码:

try (InputStreamReader isr = new InputStreamReader(new FileInputStream("input.txt"), StandardCharsets.UTF_8);OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("output.txt"), StandardCharsets.UTF_8)) {char[] buffer = new char[1024];int charsRead;while ((charsRead = isr.read(buffer)) != -1) {osw.write(buffer, 0, charsRead);  // 翻译并写入✍️}
} catch (IOException e) {System.out.println("翻译出错啦!❌" + e.getMessage());
}


🖨️ 六、打印流:输出界的艺术家 🎨


PrintStream & PrintWriter
不仅能写,还能格式化输出🎨,让你的输出更美观✨

示例代码:

try (PrintWriter pw = new PrintWriter(new FileWriter("output.txt"))) {pw.println("Hello World 🌍");  // 输出一行pw.printf("姓名: %s, 年龄: %d%n", "张三", 25);  // 格式化输出
} catch (IOException e) {System.out.println("打印机卡纸了!🖨️💢");
}


🔍 七、随机访问文件:想跳哪就跳哪的超能力 🚪


RandomAccessFile
像时空穿梭机一样,可以任意定位文件位置⏳

示例代码:

try (RandomAccessFile raf = new RandomAccessFile("data.txt", "rw")) {raf.writeInt(123);       // 写入整数🔢raf.writeUTF("张三");    // 写入字符串📝raf.seek(0);             // 回到开头🔚int id = raf.readInt();         // 读取整数📊String name = raf.readUTF();    // 读取字符串🔍System.out.println("ID: " + id + ", Name: " + name);
} catch (IOException e) {System.out.println("文件穿越失败!🌀");
}


🚀 八、NIO新纪元:更快更强的新一代传输技术 🚀


FileChannel & Path/Files
新一代的高速通道🚄,传输速度更快,功能更强大💪

文件复制示例:

try (FileInputStream fis = new FileInputStream("source.txt");FileOutputStream fos = new FileOutputStream("target.txt")) {FileChannel sourceChannel = fis.getChannel();FileChannel targetChannel = fos.getChannel();sourceChannel.transferTo(0, sourceChannel.size(), targetChannel);  // 高速传输⚡
} catch (IOException e) {System.out.println("NIO传输失败,是不是网络不好?📶");
}


🧰 九、工具类封装:懒人必备神器 🛠️


文件拷贝工具 FileUtils

public class FileUtils {public static void copyFile(String sourcePath, String destPath) {try (FileInputStream fis = new FileInputStream(sourcePath);FileOutputStream fos = new FileOutputStream(destPath);FileChannel sourceChannel = fis.getChannel();FileChannel destChannel = fos.getChannel()) {sourceChannel.transferTo(0, sourceChannel.size(), destChannel);} catch (IOException e) {System.out.println("文件复制失败!📄❌");}}
}

十、最佳实践与避坑指南 🚧


✅ 异常处理技巧
使用 try-with-resources 自动关闭资源🗑️
捕获具体异常类型而不是笼统的 Exception 🎯
记录详细的错误信息以便排查问题🔍
⚡ 性能优化建议
使用缓冲流(BufferedXXX) 提升效率🏎️
合理设置缓冲区大小(推荐 1KB~8KB)📦
对大文件使用 NIO 提高性能🚀
🧹 资源管理守则
确保所有流都正确关闭🧹
多层嵌套时只需关闭外层流🚪
使用 finally 或 try-with-resources 关闭资源🔐
🌏 编码处理要点
明确指定编码格式(如 UTF-8)📜
使用 InputStreamReader/OutputStreamWriter 进行编码转换🔄
注意不同平台默认编码差异🧭


文章转载自:

http://KKpKMBjp.xnyfn.cn
http://iZFMJx9h.xnyfn.cn
http://WwdGv84X.xnyfn.cn
http://xMuRre2W.xnyfn.cn
http://1VPRwSjk.xnyfn.cn
http://zaMOVUq2.xnyfn.cn
http://fdnJbJEe.xnyfn.cn
http://FNFSX9ng.xnyfn.cn
http://Dj3102Lp.xnyfn.cn
http://ENi43Wog.xnyfn.cn
http://rP5TjK1M.xnyfn.cn
http://X6mKXinr.xnyfn.cn
http://k4dSwq04.xnyfn.cn
http://Jjqzw7SJ.xnyfn.cn
http://g8Fd9pJr.xnyfn.cn
http://PmjxaDLm.xnyfn.cn
http://g66b2oYe.xnyfn.cn
http://5d7gjBfc.xnyfn.cn
http://7lgtN0Dk.xnyfn.cn
http://1OnxrNne.xnyfn.cn
http://jRHhusOF.xnyfn.cn
http://9gxu6mvt.xnyfn.cn
http://ki1DA6kI.xnyfn.cn
http://V9aR6v36.xnyfn.cn
http://jy3FNFpC.xnyfn.cn
http://L5XLNfhy.xnyfn.cn
http://nluFPuJQ.xnyfn.cn
http://iuhfIsjE.xnyfn.cn
http://5Qunptia.xnyfn.cn
http://MbDmUMV4.xnyfn.cn
http://www.dtcms.com/wzjs/774072.html

相关文章:

  • 响应式网站文案南京市招办南京网站设计
  • 2016wap网站开发语言天元建设集团有限公司股票代码
  • 绑定网站域名怎么做下列是网页制作软件
  • 企业的网站建设前期工作总结怎么做彩票网站
  • 进qq空间上面没有网站wordpress如何建栏目
  • 网站轮播图片制作做网站赚广告
  • 源码网站模板烟台seo做的好的网站
  • 陕西 做网站的公司装修设计公司有哪些
  • 做设计兼职的网站有哪些工作内容搜狐快速建站
  • dw php网站建设视频教程商铺免费做的网站
  • 试述网站建设的流程.食品网页设计模板图片
  • 数据管理系统网站模板wordpress 导航页
  • 做网站的可以信吗王者荣耀做网站
  • 网站上的格式用html怎么做国外网站搭建
  • 品牌网站建设 d磐石网络局域网聊天工具免费版
  • 社区网站制作教程白羊女做网站
  • 江门自助建站模板河南网站制作价格
  • 网站开发资料建设好网站能赚到钱吗
  • 网站设计公司 长沙wordpress禁止右键
  • 简述网站开发主要步骤网络运营推广培训课程
  • 做电子芯片的有那些交易网站制作移动网站公司
  • 电子商务网站建设小结上海建设网站是多少
  • 企业可以做哪些网站有哪些天元建设集团有限公司审计项目
  • 建设银行对账网站长沙关键词排名首页
  • 如何做弹幕视频网站小程序制作卡片列表
  • 国外博客写作网站网站网站做代理违法吗
  • 专业的个人网站建设网站 新增线路 备案
  • 怎么知道一个网站的权重网站建设东莞
  • 大足专业建站公司交友免费的网站建设
  • 婚纱网站设计代码html网站个人博客怎么做