Java 文件读写操作
2016-12-23 14:24
393 查看
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(); } } }
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- Debian 7.x 安装Oracle JAVA
- springmvc实现url路由功能
- spring boot 配置 druid/** * 配置druid * Created by adam on 4/11/16. */ @Configuration public class D
- api接口rsa加密
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本