java按指定编码写入和读取文件内容的类

可以指定编码如:utf-8来写入和读取文件。如果文件编码未知,可以通过该方法先得到文件的编码后再指定正确的编码来读取,否则会出现文件乱码问题。

如何识别文件编码请参考:java自动根据文件内容的编码来读取避免乱码

  1. package com.zuidaima.util; 
  2.  
  3. import java.io.BufferedReader; 
  4. import java.io.BufferedWriter; 
  5. import java.io.File; 
  6. import java.io.FileInputStream; 
  7. import java.io.FileOutputStream; 
  8. import java.io.IOException; 
  9. import java.io.InputStreamReader; 
  10. import java.io.OutputStreamWriter; 
  11.  
  12. public class ReadWriteFileWithEncode { 
  13.  
  14.     public static void write(String path, String content, String encoding) 
  15.             throws IOException { 
  16.         File file = new File(path); 
  17.         file.delete(); 
  18.         file.createNewFile(); 
  19.         BufferedWriter writer = new BufferedWriter(new OutputStreamWriter( 
  20.                 new FileOutputStream(file), encoding)); 
  21.         writer.write(content); 
  22.         writer.close(); 
  23.     } 
  24.  
  25.     public static String read(String path, String encoding) throws IOException { 
  26.         String content = ""
  27.         File file = new File(path); 
  28.         BufferedReader reader = new BufferedReader(new InputStreamReader( 
  29.                 new FileInputStream(file), encoding)); 
  30.         String line = null
  31.         while ((line = reader.readLine()) != null) { 
  32.             content += line + "\n"
  33.         } 
  34.         reader.close(); 
  35.         return content; 
  36.     } 
  37.  
  38.     public static void main(String[] args) throws IOException { 
  39.         String content = "中文内容"
  40.         String path = "c:/test.txt"
  41.         String encoding = "utf-8"
  42.         ReadWriteFileWithEncode.write(path, content, encoding); 
  43.         System.out.println(ReadWriteFileWithEncode.read(path, encoding)); 
  44.     } 
  45.  
  46.                      
package com.zuidaima.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class ReadWriteFileWithEncode {

	public static void write(String path, String content, String encoding)
			throws IOException {
		File file = new File(path);
		file.delete();
		file.createNewFile();
		BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
				new FileOutputStream(file), encoding));
		writer.write(content);
		writer.close();
	}

	public static String read(String path, String encoding) throws IOException {
		String content = "";
		File file = new File(path);
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				new FileInputStream(file), encoding));
		String line = null;
		while ((line = reader.readLine()) != null) {
			content += line + "\n";
		}
		reader.close();
		return content;
	}

	public static void main(String[] args) throws IOException {
		String content = "中文内容";
		String path = "c:/test.txt";
		String encoding = "utf-8";
		ReadWriteFileWithEncode.write(path, content, encoding);
		System.out.println(ReadWriteFileWithEncode.read(path, encoding));
	}
}

	    			



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值