JAVA写文件问题 用java怎么向文件指定行中追加内容,例如源文件内容:12345678在第三行追加"sss"想得到的内容:123456sss78 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://hi.baidu.com/zdz8207/blog/item/7f26d3a21ed1a8abcbefd0d9.html搜索“java怎么向文件指定行中追加内容”的第一条记录 package io;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileWriter;import java.io.InputStreamReader;import java.io.PrintWriter;public class SuperAdd { public void fox_Split(String txtIn, String txtOut) { int rowNum=0; File fileIn = new File(txtIn); File[] txtFiles = fileIn.listFiles(); for (int i = 0; i < txtFiles.length; i++) { File txtFile = txtFiles[i]; try { FileInputStream fis = new FileInputStream(txtFile); InputStreamReader isr = new InputStreamReader(fis); BufferedReader br = new BufferedReader(isr); File file = new File(txtOut + File.separator); if (!file.exists()) { file.mkdirs(); } while (br.ready()) { rowNum++; String s = br.readLine(); if(rowNum==3){ s = s+"sss"; } PrintWriter txtPw = new PrintWriter(new FileWriter(txtOut + File.separator + "Txt_Change" + ".txt", true)); txtPw.println(s); txtPw.close(); } br.close(); isr.close(); fis.close(); } catch (Exception e) { e.printStackTrace(); } } System.out.println(rowNum); } public static void main(String[] args) { String txtIn = "E:\\Fox\\Fox1\\"; String txtOut = "E:\\Fox\\Fox2\\"; SuperAdd superAdd = new SuperAdd(); superAdd.fox_Split(txtIn, txtOut); }} 好像只有追加到文件尾部用RandomAccessFile,会覆盖文件后面的内容 RandomAccessFile貌似可以从具体某一行开始 先读出来,然后通过readline找到你要追加的行数,然后修改再覆盖咯。。 有没有效率高一点的,RandomAccessFile不能直接定位到行 import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import java.io.PrintWriter;public class RowAppend { public static void main(String[] args) throws Exception{ File file = new File("F:\\java\\Append.txt"); BufferedReader br = new BufferedReader(new FileReader(file)); int temp = 0; int b = 0; while(br.ready()){ temp++; String str = br.readLine(); if (temp == 3) { str= str + "sss"; } PrintWriter txtPw = new PrintWriter( new FileWriter("F:\\java\\Append1.txt", true)); txtPw.println(str); txtPw.close(); } br.close(); System.out.println(temp); }} 请测试!我测试通过!没有问题!上面发的稍微有点问题,这次的没问题了!同一文本内容的操作!import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import java.util.ArrayList;import java.util.List;/*问题是,往F:\\java\\Append.txt文本中的第3行的数字后面加入AAA,用java解决 不能把东西输出到别的地方,只能是在F:\\java\\Append.txt里改动。 /*注释:F:\\java\\Append.txt 文本中的内容原本为: 11 22 33 44 55 */public class RowAppend { public static void main(String[] args) throws Exception { File file = new File("F:\\java\\Append.txt"); BufferedReader br = null;// 加缓冲的文件字符输入流 br = new BufferedReader(new FileReader(file)); List<String> list = new ArrayList<String>(); int temp = 0; while (br.ready()) { String str = br.readLine(); temp++; if (temp == 3) { str = str + "AAA"; } list.add(str); } br.close(); BufferedWriter bw = null;// 加缓冲的文件字符输出流 bw = new BufferedWriter(new FileWriter(file)); for (String str : list) { bw.write(str); bw.newLine(); System.out.println(str); } bw.close(); System.out.println(temp); }} 怎么获取Axis2 异步调用的返回值 第二个循环怎么出现数组越界异常? 新手求助:网络编程套接字之郁闷 命令javac 与 java为什么只能在一个文件夹下有效呢? 问一个很无聊的问题,为什么JDK里面Arrays类,基本类型用快速排序,而剩下的就用归并排序呢? 请大家指点,万分感激! 求助键盘 接收字符或是数字 Linux 下javac 编译的问题,高分求解..... 为什么不能编译?? 能不能把byte数组的内容直接以String 的形式输出 求问protected方法和友好方法的区别 用java给固定文本中固定行数追加三个A。有详细说明,高手来!
搜索“java怎么向文件指定行中追加内容”的第一条记录
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.PrintWriter;public class SuperAdd {
public void fox_Split(String txtIn, String txtOut) {
int rowNum=0;
File fileIn = new File(txtIn);
File[] txtFiles = fileIn.listFiles();
for (int i = 0; i < txtFiles.length; i++) {
File txtFile = txtFiles[i];
try {
FileInputStream fis = new FileInputStream(txtFile);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr); File file = new File(txtOut + File.separator);
if (!file.exists()) {
file.mkdirs();
}
while (br.ready()) {
rowNum++;
String s = br.readLine();
if(rowNum==3){
s = s+"sss";
}
PrintWriter txtPw = new PrintWriter(new FileWriter(txtOut
+ File.separator + "Txt_Change" + ".txt", true));
txtPw.println(s);
txtPw.close(); }
br.close();
isr.close();
fis.close(); } catch (Exception e) {
e.printStackTrace();
}
}
System.out.println(rowNum);
} public static void main(String[] args) {
String txtIn = "E:\\Fox\\Fox1\\";
String txtOut = "E:\\Fox\\Fox2\\"; SuperAdd superAdd = new SuperAdd();
superAdd.fox_Split(txtIn, txtOut);
}
}
用RandomAccessFile,会覆盖文件后面的内容
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
public class RowAppend {
public static void main(String[] args) throws Exception{
File file = new File("F:\\java\\Append.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
int temp = 0;
int b = 0;
while(br.ready()){
temp++;
String str = br.readLine();
if (temp == 3) {
str= str + "sss";
}
PrintWriter txtPw = new PrintWriter(
new FileWriter("F:\\java\\Append1.txt", true));
txtPw.println(str);
txtPw.close();
}
br.close();
System.out.println(temp);
}
}
上面发的稍微有点问题,这次的没问题了!同一文本内容的操作!import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;/*问题是,往F:\\java\\Append.txt文本中的第3行的数字后面加入AAA,用java解决
不能把东西输出到别的地方,只能是在F:\\java\\Append.txt里改动。 /*注释:F:\\java\\Append.txt 文本中的内容原本为:
11
22
33
44
55
*/
public class RowAppend {
public static void main(String[] args) throws Exception {
File file = new File("F:\\java\\Append.txt");
BufferedReader br = null;// 加缓冲的文件字符输入流
br = new BufferedReader(new FileReader(file)); List<String> list = new ArrayList<String>();
int temp = 0;
while (br.ready()) {
String str = br.readLine();
temp++;
if (temp == 3) {
str = str + "AAA";
}
list.add(str);
}
br.close(); BufferedWriter bw = null;// 加缓冲的文件字符输出流
bw = new BufferedWriter(new FileWriter(file)); for (String str : list) {
bw.write(str);
bw.newLine();
System.out.println(str);
}
bw.close();
System.out.println(temp);
}
}