我知道已经有一些关于这个问题的帖子,但我不了解它们。 我的问题是我想在带有名称的txt文档中找到一行,然后我想将下一行更改为字符串的内容。
这就是我的尝试:
public void saveDocument(String name) {
String documentToSave = textArea1.getText();
File file = new File("documents.txt");
Scanner scanner;
BufferedWriter bw;
try {
scanner = new Scanner(file);
bw = new BufferedWriter(new FileWriter(file));
while(scanner.hasNextLine()) {
if(scanner.nextLine().equals(name)) {
if(scanner.hasNextLine()) bw.write(scanner.nextLine() + "\n");
bw.write(documentToSave + "\n");
if(scanner.hasNextLine()) scanner.nextLine();
}
if(scanner.hasNextLine()) bw.write(scanner.nextLine() + "\n");
}
bw.flush();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
答案 0 :(得分:0)
此代码应读取整个文件并用名称替换该行。
Dictionary
答案 1 :(得分:0)
此程序将替换给定行之后的行。它需要我们的更多工作,例如,如果我们可以定义预期的i / o和使用。现在它从文件中读取并重新放置一行,但是您可能希望使用行号而不是行内容来标记要替换的行。
import java.io.*;
public class FileLineReplace {
public static void replaceSelected(String replaceWith, String type) {
try {
String input2 = "";
boolean replace = false;
String input = "";
try (BufferedReader br = new BufferedReader(new FileReader("data.txt"))) {
for (String line;
(line = br.readLine()) != null;) {
// process the line.
System.out.println(line); // check that it's inputted right
if (replace) {
line = "*** REPLACED ***";
replace = false;
}
if (line.indexOf("replaceNextLine") > -1) {
replace = true;
}
input2 = input2 += (line + "\n");
}
// line is not visible here.
}
// check if the new input is right
System.out.println("----------------------------------" + '\n' + input2);
// write the new String with the replaced line OVER the same file
FileOutputStream fileOut = new FileOutputStream("data.txt");
fileOut.write(input2.getBytes());
fileOut.close();
} catch (Exception e) {
System.out.println("Problem reading file.");
e.printStackTrace();
}
}
public static void main(String[] args) {
replaceSelected("1 adam 20 M", "foobar");
}
}
如果您运行代码,它将在" replaceNextLine":
之后的行之后替换下一行。$ java FileLineReplace
asd
zxc
xcv
replaceNextLine
wer
qwe
----------------------------------
asd
zxc
xcv
replaceNextLine
*** REPLACED ***
qwe
我的测试文件是(是)
asd
zxc
xcv
replaceNextLine
wer
qwe
运行程序后,文件看起来像这样,并且替换了指定行之后的行。
asd
zxc
xcv
replaceNextLine
*** REPLACED ***
qwe
答案 2 :(得分:0)
可能是这样尝试:读取文件并将每行保留在字符串列表中,如果找到要查找的名称,请替换下一行。然后将该列表中的字符串写回您的文件。示例:
public static void Avreg(SqlCommand command1, string s, string t,
string p, string c, string d)
{
var query = "UPDATE....";
if (c == "0")
{
query += ", CitizenshipDate = null";
}
else
{
query += ", CitizenshipDate = @CitizenshipDate";
if(!command1.Parameters.Contains("@CitizenshipDate"))
command1.Parameters.Add("@CitizenshipDate", SqlDbType.NVarChar);
command1.Parameters["@CitizenshipDate"].Value = string.IsNullOrEmpty(c) ? DBNull.Value : (object)c);
}
.....
}
}
txt.txt
public class NewClass {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list = readFile("uzochi");
writeToFile(list);
}
public static List<String> readFile(String name){
List<String> list = new ArrayList<String>();
try {
FileReader reader = new FileReader("C:\\users\\uzochi\\desktop\\txt.txt");
BufferedReader bufferedReader = new BufferedReader(reader);
String line;
boolean nameFound = false;
while ((line = bufferedReader.readLine()) != null) {
if(line.equalsIgnoreCase(name)){
nameFound = true;
System.out.println("searched name: "+line);
}
if(nameFound){
list.add(line);
line = bufferedReader.readLine();
System.out.println("line to replace: " + line);
line = "another string";
System.out.println("replaced line: "+line);
list.add(line);
nameFound = false;
}
else{
list.add(line);
}
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
public static void writeToFile(List<String> list){
try {
FileWriter writer = new FileWriter("C:\\users\\uzochi\\desktop\\txt.txt", false);
BufferedWriter bufferedWriter = new BufferedWriter(writer);
for(String s: list){
bufferedWriter.write(s);
bufferedWriter.newLine();
}
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}