Java 文件读写操作
来源:互联网 发布:csoldjb9.0最终优化 编辑:程序博客网 时间:2024/06/03 02:59
1. 对于文本文件的读写
1.1. 读取文本文件
package com.martinwangjun.io;import java.io.FileReader;import java.io.IOException;public class FileReaderTest { // 注意,需要抛出IOException或在代码中使用try...catch... public static void main(String[] args) throws IOException { // 对于eclipse,注意实际的路径,我们这里用了package FileReader fileReader = new FileReader( "./src/com/martinwangjun/io/FileReaderTest.java"); char[] buffer = new char[1024]; int hasReader = 0; while ((hasReader = fileReader.read(buffer)) > 0) { System.out.println(new String(buffer, 0, hasReader)); } // 这里其实写得不严谨,最好使用Java 7的AutoClosable // 或者finally中关闭资源 fileReader.close(); }}
1.2. 写入文本文件
package com.martinwangjun.io;import java.io.FileWriter;public class FileWriterTest { public static void main(String[] args) { // 这里用来Java 7的AutoClosable的特性,会自动关闭资源。 try(FileWriter fileWriter = new FileWriter( "src/com/martinwangjun/io/poem.txt")) { fileWriter.write("先令\r\n"); } catch (Exception ex) { ex.printStackTrace(); } }}
2. 二进制文件
package com.martinwangjun.io;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;public class FileOutputStreamTest { public static void main(String[] args) throws IOException{ // 使用了Java 7的自动关闭机制 try(FileInputStream fileInputStream = new FileInputStream( "src/com/martinwangjun/io/01.jpg"); FileOutputStream fileOutputStream = new FileOutputStream( "src/com/martinwangjun/io/02.jpg"); ){ byte[] buffer = new byte[1024]; int hasRead = 0; while ((hasRead = fileInputStream.read(buffer)) > 0) { fileOutputStream.write(buffer, 0, hasRead); } buffer = "Powered By Martin".getBytes(); fileOutputStream.write(buffer, 0, "Powered By Martin".length()); } catch (Exception ex) { ex.printStackTrace(); } }}
3. 利用包装流
3.1. 利用PrintStream流来包装FileOutputStream
package com.martinwangjun.io;import java.io.FileOutputStream;import java.io.IOException;import java.io.PrintStream;public class PrintStreamTest { public static void main(String[] args) throws IOException { try( FileOutputStream fos = new FileOutputStream( "src/com/martinwangjun/io/test.txt"); PrintStream ps = new PrintStream(fos)) { // 这样的话不用考虑怎么写入文件,当成普通的打印一样使用 ps.println("普通字符串"); ps.println(new PrintStreamTest()); } catch (Exception ex) { ex.printStackTrace(); } }}
4. 获取用户输入
package com.martinwangjun.io;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class KeyinTest { public static void main(String[] args) throws IOException{ InputStreamReader reader = null; BufferedReader bufferedReader = null; try { reader = new InputStreamReader(System.in); bufferedReader = new BufferedReader(reader); String line = null; while ((line = bufferedReader.readLine()) != null) { if(line.toLowerCase().equals("exit")){ System.exit(1); } System.out.println(line); } } catch (Exception ex) { ex.printStackTrace(); } finally { if(reader != null){ try { reader.close(); } catch (Exception ex) { ex.printStackTrace(); } } if(bufferedReader != null){ try { bufferedReader.close(); } catch (Exception ex) { ex.printStackTrace(); } } } }}
5. 添加而不是覆盖
package com.martinwangjun.io;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileOutputStream;import java.io.InputStreamReader;import java.io.OutputStreamWriter;public class BufferedWriteTextFile { public static void main(String[] args) throws Exception { try ( // 这里用了true,表示可以Append BufferedWriter bufferedWriter = new BufferedWriter( new OutputStreamWriter( new FileOutputStream( "src/com/martinwangjun/io/output.txt", true))); BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(System.in))) { String line = null; while( ! (line = bufferedReader.readLine()).equals("")){ bufferedWriter.write(line); bufferedWriter.newLine(); } } catch (Exception e) { e.printStackTrace(); } }}
0 0
- java 读写文件操作
- java读写文件操作
- Java文件读写操作
- JAVA读写文件操作
- java文件读写操作
- Java文件读写操作
- java文件读写操作
- java 文件读写 操作
- java 文件读写操作
- JAVA读写文件操作
- java文件读写操作
- 文件读写操作-Java
- Java --- 读写文件操作
- Java文件读写操作
- Java文件读写操作
- Java文件读写操作
- java文件读写操作
- Java文件读写操作
- 将 "arguments" 转变为 Array 的多种姿势
- VR行业解决方案
- throws如何用
- iOS中用swift转换日期的几个通用函数
- JAVA动态代理 和 Spring AOP 4种通知的简单实现
- Java 文件读写操作
- 各种排序算法思想总结
- libsvm最新源代码(版本3.21)理解解析(一)
- 寻找和为定值的N个数
- 前端面试题搜集之一——理论知识
- CMAKE学习笔记(二)
- microsoft query尚未安装的原因和方法
- Retrofit2使用案例及源码解析
- Spring整合Mongodb并插入数据演示