文件信息的读取和写入

来源:互联网 发布:mac修容粉价格 编辑:程序博客网 时间:2024/05/31 18:37

第一种

import java.io.File;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Work01 {

public static void main(String[] args) {
// 1. 使用FileReader和FileWriter读取和写入txt文件信息(内容不限)
//FileReader字符流读取文件
//一、根据磁盘上的文件路径创建一个file对象,使用File必须引入java.io.File
File file = new File("F:\\work\\io.txt");

//二、判断这个文件是否存在,使用exists()这个方法
if(!file.exists()){//当文件不存在时,先把这个文件创建出来,使用createNewFile()方法。
try {
file.createNewFile();
} catch (IOException e) {
// 捕捉异常
e.printStackTrace();
}
}

//文件存在的情况下在读取文件,否则会发生空指针异常
//字符流读取文件信息
try {
FileReader fr =new FileReader(file);
int i = -1;
try {
while((i = fr.read()) != -1){
System.out.print((char)i);
}
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}

//字符流写入信息
try {
//多加一个true,防止放生覆盖,从文件末尾开始追加
FileWriter fw = new FileWriter(file,true);
String str = "我正在学习I/O流!";
//fw.write(str);会发生默认覆盖
fw.write(str);
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}


第二种

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Work02 {

public static void main(String[] args) {
/*
* 2. 使用BufferedReader类和BufferedWriter类 
*    缓冲流
*    读文件方式
*    File f = new File("d:/a.txt");
*    FileReader fr = new FileReader(f);
*    BufferedReader br = new BufferedReader(fr);
*    读出一行:String info = br.readLine();
*    
*    写文件方式
*    File f = new File("d:/b.txt");
*    FileWriter fw = new FileWriter(f2);
*    BufferedWriter bw = new BufferedWriter(fw);
*    写入一行:bw.write(info+"\r\n");
*    \r表示转义字符
*    (注意:\r\n 表示换行)
     */
//创建文件对象
File f = new File("d:\\a.txt");
//判断文件是否存在
if(! f.exists()){//文件不存在,创建一个文件
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
//文件存在,如果文件是新创建的文件,则内容为空
try {
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String str = null;//用来接收缓冲流读取的字符串
try {
FileWriter fw1 = new FileWriter(f,true);
BufferedWriter bw1 = new BufferedWriter(fw1);
bw1.write("文件一");
while((str = br.readLine()) != null){
System.out.println(str);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
  
//创建文件对象
File file = new File("d:\\b.txt");
//判断文件是否存在
if(! file.exists()){//文件不存在,创建一个文件
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
//文件存在
try {
FileWriter fw = new FileWriter(file,true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write("info"+"\r\n");
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}

第三种

public class Work03 {

public static void main(String[] args) {
//3.题目:有2个文本文件x和y,要求将x文件中的信息和y中的信息全部写入z文件中,并且y中的信息在x的后面。
//一、先创建三个文件对象
File file1 = new File("F:\\work\\x.txt");
File file2 = new File("F:\\work\\y.txt");
File file3 = new File("F:\\work\\z.txt");

//二、判断三个文件是否存在
if(!file1.exists()){
try {
file1.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
if(!file2.exists()){
try {
file2.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
if(!file3.exists()){
try {
file3.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}

//三个文件都存在

try {
//文件输入流   读取文件信息
FileInputStream fileIn1 = new FileInputStream(file1);
FileInputStream fileIn2 = new FileInputStream(file2);
//文件输出流  给文件写入信息
FileOutputStream fileOut3 = new FileOutputStream(file3,true);

int i = -1;
try {
while((i = fileIn1.read()) != -1){
//把x.txt中的信息写入z.txt
fileOut3.write(i);

}
fileIn1.close();
i = -1;
while((i = fileIn2.read()) != -1){
//把y.txt中的信息写入z.txt
fileOut3.write(i);
}
fileIn2.close();
fileOut3.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

}