java 文件指定位置插入_java 中利用io流将一个文件插入到另一个文件中的指定位置,指定位置...
展開全部
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 文件指定位置插入_java 中利用io流将一个文件插入到另一个文件中的指定位置,指定位置...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 爬虫 百度新闻_基于HttpC
- 下一篇: java查找最小字符集_如何查找Java