在Java中写入指定行?
为了澄清我的问题,我正在给电脑上的一个文本文件写信,该文件中已经有文本内容。下面的代码写入文本文件而不删除其任何内容,但现在我想指定它写入的行。当前,它有一个if语句来捕获行中是否存在单词“Done”,因为它确实写入文件,所以可以识别它。我的问题是试图让它写到“完成”的那一行。它写入代码的最底层,最后一行。我怎样才能解决这个问题 readNumLines方法是文件中包含的行数。Reading是文本文件名在Java中写入指定行?,java,file,text,printing,writing,Java,File,Text,Printing,Writing,为了澄清我的问题,我正在给电脑上的一个文本文件写信,该文件中已经有文本内容。下面的代码写入文本文件而不删除其任何内容,但现在我想指定它写入的行。当前,它有一个if语句来捕获行中是否存在单词“Done”,因为它确实写入文件,所以可以识别它。我的问题是试图让它写到“完成”的那一行。它写入代码的最底层,最后一行。我怎样才能解决这个问题 readNumLines方法是文件中包含的行数。Reading是文本文件名 int i = 0; BufferedReader reader = new Buf
int i = 0;
BufferedReader reader = new BufferedReader(new FileReader(path+reading));
BufferedWriter writer = new BufferedWriter(new FileWriter(path+reading, true));
String line = null;
while ((line = reader.readLine()) != null && i < readNumLines(reading)-1) {
if(line.contains("Done")){
writer.write("\nCHECK!\n");
writer.close();
}
i++;
}
inti=0;
BufferedReader reader=新BufferedReader(新文件读取器(路径+读取));
BufferedWriter-writer=新的BufferedWriter(新文件写入程序(路径+读取,true));
字符串行=null;
while((line=reader.readLine())!=null&&i
我建议将整个文件读入一个变量(字符串、数组或列表)。您可以轻松地修改特定行,然后将所有内容写回文件
1) 正在将文件读入数组:
2) 操纵数组:
for (int i = 0; i < lines.length; i++) {
String line = lines[i];
if (line.contains("Done")) {
lines[i] = line + "\nCHECK!\n";
break;
}
}
第一个“\n”强制将“检查”写入新行。这假设您的“完成”行是最后一行,而不是以“\n”结尾。这是我的做法。方法很简单,将所有内容(包括新行)写入临时文件,然后将临时文件重命名为原始文件并删除原始文件。注意:我从我的一个项目中提取了这个代码staigt,因此我可能忘记了更改某些内容。所以,如果它不起作用,让我知道,我会看看。希望这有帮助:)
使用Java 8时,您可以尝试以下方法:
final Path myFile = Paths.get("path\\to\\yourFile.txt");
final int lineNumWhereToInsert = 2;
final String stringToInsert = "insertion test";
try {
final List<String> lines = Files.lines(myFile).collect(Collectors.toList());
lines.add(Math.min(lineNumWhereToInsert, lines.size()), stringToInsert);
try (final BufferedWriter out = Files.newBufferedWriter(myFile, Charset.forName("UTF-8"))) {
for (final String line : lines) {
out.append(line).append(System.lineSeparator());
}
}
} catch (IOException) {
ex.printStrackTrace();
}
final Path myFile=Path.get(“Path\\to\\yourFile.txt”);
最终int lineNumWhereToInsert=2;
最终字符串stringToInsert=“插入测试”;
试一试{
最终列表行=Files.lines(myFile.collect(Collectors.toList());
添加(Math.min(lineNumWhereToInsert,lines.size()),stringToInsert);
try(final BufferedWriter out=Files.newBufferedWriter(myFile,Charset.forName(“UTF-8”)){
用于(最后一行字符串:行){
out.append(line.append(System.lineSeparator());
}
}
}捕获(IOException){
例如printStrackTrace();
}
如果两个实例都指向同一个文件,我认为您不能读写同一个文件。您的FileWriter是否处于追加模式?是,尝试在特定位置读取和写入文件时处于附加模式。我以前想过这一点,但希望有更简单的方法。如果文件确实很大,您可能必须使用其他解决方案,因为无法完全加载它。但这个解决方案可能不会更简单。实际上,这不起作用,我正在Linux上运行,似乎renameTo现在不适合我。奇怪的是,我真的不知道Linux是如何工作的,所以如果它与Linux有关,我不知道,对不起。哪里有错误?奇怪的是没有,它甚至没有重命名文件,我读了进去。人们说它非常面向平台,所以所有东西都被正确地写入文件,只是没有正确地重命名?因为在这种情况下,您可能会找到一种正确重命名文件的方法。我将继续四处查看,有人说请尝试file.move,这样我就可以将文件放在系统中的某个位置,然后将其移动到需要的位置。
String line;//You need to specify those
File infile;
// temp file
File outFile = new File("$$$$$$$$.tmp");
// input
FileInputStream fis = null;
try {
fis = new FileInputStream(inFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BufferedReader in = new BufferedReader(new InputStreamReader(fis));
// output
FileOutputStream fos = null;
try {
fos = new FileOutputStream(outFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
PrintWriter out = new PrintWriter(fos);
String thisLine = "";
int i =1;
try {
while ((thisLine = in.readLine()) != null) {
if(in.contains("Done"))//the check for done
out.println(line);
}
out.println(thisLine);
i++;
}
} catch (IOException e) {
e.printStackTrace();
}
out.flush();
out.close();
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
inFile.delete();
outFile.renameTo(inFile);
final Path myFile = Paths.get("path\\to\\yourFile.txt");
final int lineNumWhereToInsert = 2;
final String stringToInsert = "insertion test";
try {
final List<String> lines = Files.lines(myFile).collect(Collectors.toList());
lines.add(Math.min(lineNumWhereToInsert, lines.size()), stringToInsert);
try (final BufferedWriter out = Files.newBufferedWriter(myFile, Charset.forName("UTF-8"))) {
for (final String line : lines) {
out.append(line).append(System.lineSeparator());
}
}
} catch (IOException) {
ex.printStrackTrace();
}