展开全部
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
public class Demo {
636f707962616964757a686964616f31333335333062public static void main(String[] args) throws Exception{
String text = getReader("D:\\1.txt");
getWriter("D:\\2.txt",text,2,1);
}
/**
* @param pathName : 源文件路径
**/
private static String getReader(String pathName)throws Exception{
File file = new File(pathName);
if(!file.exists())
throw new RuntimeException("文件不存在!");
BufferedReader fr = new BufferedReader(new FileReader(file));
StringBuilder sb = new StringBuilder();
String str = null;
while((str=fr.readLine())!=null){
sb.append(str+"\r\n");
}
fr.close();
return sb.toString();
}
/**
* @param pathName : 要copy的文件路径
* @param text : 叠加的内容
* @param x : 行,从0开始
* @param y : 列,从0开始
**/
private static void getWriter(String pathName,String text,int x, int y)throws Exception{
File file = new File(pathName);
if(!file.exists())
throw new RuntimeException("文件不存在!");
BufferedReader fr = new BufferedReader(new FileReader(file));
StringBuilder sb = new StringBuilder();
for(int i = 0; i
sb.append(fr.readLine()+"\r\n");
}
for(int i = 0; i
sb.append((char)fr.read());
}
sb.append(text);
String str = null;
while((str=fr.readLine())!=null){
sb.append(str);
}
fr.close();
FileWriter fw = new FileWriter(file);
fw.write(sb.toString());
fw.close();
}
}
//测试文件1.txt内容:
asd
sss
aaa
//测试文件2.txt内容:
111
222
3asd
sss
aaa
33
本文介绍了一个使用Java实现的简单文件操作程序,包括从指定路径读取文件内容,并将内容复制到另一个文件中,同时在指定位置插入额外文本的功能。通过 BufferedReader 和 FileWriter 类实现了文件的读取和写入。

被折叠的 条评论
为什么被折叠?



