(字节流、字符流)对文件进行读写操作
2017-03-25 00:51
309 查看
/*
* 使用字符流对文件进行读写操作
*/
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class T04 {
public static void main(String[] args) throws Exception{
String value = “中国风\n”;
String value2 = “a 中国风\n”;
// 向文件中写入内容
PrintWriter pw = new PrintWriter(“temp.txt”,”UTF-8”);
pw.write(value);
pw.write(value2);
pw.close();
// 从文件中读取内容
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(“temp.txt”),”utf-8”));
String b;
while((b = br.readLine())!=null){ // 按行读取
System.out.println(b);
}
br.close();
}
}
运行结果:
中国风
a 中国风
/*
* 使用字节流对文件进行读写操作
*/
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.File;
public class T05 {
public static void main(String[] args) throws Exception{
String value = “中国风\n”;
String value2 = “a 中国风\n”;
// 向文件中写入内容
File f = new File(“temp.txt”);
FileOutputStream fos = new FileOutputStream(f);
fos.write(value.getBytes(“UTF-8”)); // 可以指定编码
fos.write(value2.getBytes());
fos.close();
// 从文件中读取内容
FileInputStream fis = new FileInputStream(f);
byte b[] = new byte[(int)f.length()];
int len = fis.read(b); // 读取后返回长度
fis.close();
System.out.println(new String(b));
}
}
运行结果:
涓浗椋�
a 中国风
* 使用字符流对文件进行读写操作
*/
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class T04 {
public static void main(String[] args) throws Exception{
String value = “中国风\n”;
String value2 = “a 中国风\n”;
// 向文件中写入内容
PrintWriter pw = new PrintWriter(“temp.txt”,”UTF-8”);
pw.write(value);
pw.write(value2);
pw.close();
// 从文件中读取内容
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(“temp.txt”),”utf-8”));
String b;
while((b = br.readLine())!=null){ // 按行读取
System.out.println(b);
}
br.close();
}
}
运行结果:
中国风
a 中国风
/*
* 使用字节流对文件进行读写操作
*/
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.File;
public class T05 {
public static void main(String[] args) throws Exception{
String value = “中国风\n”;
String value2 = “a 中国风\n”;
// 向文件中写入内容
File f = new File(“temp.txt”);
FileOutputStream fos = new FileOutputStream(f);
fos.write(value.getBytes(“UTF-8”)); // 可以指定编码
fos.write(value2.getBytes());
fos.close();
// 从文件中读取内容
FileInputStream fis = new FileInputStream(f);
byte b[] = new byte[(int)f.length()];
int len = fis.read(b); // 读取后返回长度
fis.close();
System.out.println(new String(b));
}
}
运行结果:
涓浗椋�
a 中国风
相关文章推荐
- 129.(字节流、字符流)对文件进行读写操作
- 使用字符/字节流对文件进行读写操作
- 分别使用(字符流)和(字节流)对文件进行读写操作
- python使用codecs模块进行文件操作-读写中英文字符
- python使用codecs模块进行文件操作-读写中英文字符
- python使用codecs模块进行文件操作-读写中英文字符
- 通过字节流来对文件进行读写操作
- python 中对配置文件(如.ini文件)进行读写等操作ConfigParser模块
- C++中对文件进行读写操作
- C++中对文件进行读写操作
- C++中对文件进行读写操作
- python对文件进行读写操作
- 利用Oracle目录对文件进行读写操作
- 含读写及替换字符的复制文件操作
- 简单的Android对文件进行读写操作
- java中IO操作 利用文件流 过滤流对文件进行读写操作
- C:文件操作-读写字符与读写行(!!!好)
- C++中对文件进行读写操作
- 利用POI对MS Excel进行读写(可以设置写入文件的样式),利用tm-extractor对Word进行读取操作并将信息用(log4j)日志信息显示出
- C++中对文件进行读写操作