Java按指定编码写入文件内容的类
2014-02-19 11:31
375 查看
java按指定编码写入和读取文件内容的类
可以指定编码如:utf-8来写入和读取文件。如果文件编码未知,可以通过该方法先得到文件的编码后再指定正确的编码来读取,否则会出现文件乱码问题。
如何识别文件编码请参考:java自动根据文件内容的编码来读取避免乱码
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));
}
}
学java技术,首选疯狂软件
可以指定编码如:utf-8来写入和读取文件。如果文件编码未知,可以通过该方法先得到文件的编码后再指定正确的编码来读取,否则会出现文件乱码问题。
如何识别文件编码请参考:java自动根据文件内容的编码来读取避免乱码
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));
}
}
学java技术,首选疯狂软件
相关文章推荐
- java按指定编码写入和读取文件内容的类
- java按指定编码写入和读取文件内容
- java按指定编码写入和读取文件内容的类
- java按指定编码写入和读取文件内容的类
- [转]java按指定编码写入和读取文件内容的类
- java按指定编码写入和读取文件内容的类
- java按指定编码写入和读取文件内容的类
- java 逐行读取和写入文件内容,指定编码
- java按指定编码写入和读取文件内容的类分享
- java按指定编码写入和读取文件内容的类
- java按指定编码写入和读取文件内容的
- java按指定编码写入和读取文件内容的类
- java按指定编码写入和读取文件内容的类
- java字节流和字符流操作文件,指定编码写入和读取,递归创建上层目录
- java读写文件-经过多方搜索整合成我需要的功能(读写指定文件夹下面的多个指定名称的txt中的内容全部写入到一个指定的txt中)
- java在指定路径下创建文件,并写入文件内容
- Java 小例子:按指定的编码读取文本文件内容
- Java 小例子:按指定的编码读取文本文件内容
- 把内容写入指定目录指定文件的java文件工具类,支持日期格式目录名的生成
- Java_IO流_按照指定的编码向文件中写入文本