这个程序差不多完成了。但问题是当我编辑并将其放入临时文件时,编辑和未编辑的值都会被写入。因此,临时文件获取另一个值(替换)。我还想知道是否有方法可以重命名临时文件。我试着看其他问题'答案,但没有一个解决了我的问题。
import java.io.*;
import javax.swing.*;
import java.util.*;
public class SecondChild4 extends SecondParent
{
public void editFile(String sFileName, String sFileName2)
{
try
{
sFileName = "Second.csv";
sFileName2 = "Second.txt";
File nfile1 = new File("Second.csv");
File nfile2 = new File("Second.txt");
File file1 = new File("TempSecond.csv");
File file2 = new File("TempSecond.txt");
FileReader reader = new FileReader(sFileName);
FileReader reader2 = new FileReader(sFileName2);
BufferedReader br1 = new BufferedReader(reader);
BufferedReader br2 = new BufferedReader(reader2);
FileWriter twriter = new FileWriter(file1);
FileWriter twriter2 = new FileWriter(file2);
BufferedWriter tbw1 = new BufferedWriter(twriter);
BufferedWriter tbw2 = new BufferedWriter(twriter2);
String edit = "";
String edit2 = "";
String data = "";
Scanner scanner = new Scanner(nfile2);
String _btitle = JOptionPane.showInputDialog (null, "Title: ", "");
String _bauthor = JOptionPane.showInputDialog (null, "Author: ", "");
while(scanner.hasNext()){
boolean replace = false;
String str = scanner.nextLine();
if((str.contains(_btitle))&&(str.contains(_bauthor)))
{
String conv = str.toString();
System.out.println("Search found");
String btitle1 = JOptionPane.showInputDialog (null, "Replace with title: ", "");
String bauthor1 = JOptionPane.showInputDialog (null, "Replace with author: ", "");
edit = str.replaceAll(_btitle, btitle1);
edit2 = str.replaceAll(_bauthor, bauthor1);
tbw1.append(edit);
tbw1.append(",");
tbw1.append(edit2);
tbw1.append("\n");
tbw2.write(edit);
tbw2.write("\t");
tbw2.write(edit2);
tbw2.newLine();
replace = true;
//System.out.println("" +edit + "" +edit2); Test output
}
else {
//System.out.println("" +str); Test output
tbw1.append(str);
tbw1.append("\n");
tbw2.write(str);
tbw2.newLine();
}
}
tbw1.close();
tbw2.close();
br1.close();
br2.close();
nfile1.delete();
file1.renameTo(nfile1);
nfile2.delete();
file2.renameTo(nfile2);
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
编辑1:
好。想出了如何删除和重命名。问题是我必须先编辑2次才能最终删除并重命名。 (我删除了作者,因为输出有问题。)
import java.io.*;
import javax.swing.*;
import java.util.*;
public class SecondChild4 extends SecondParent
{
public void editFile(String sFileName, String sFileName2)
{
try
{
sFileName = "Second.csv";
sFileName2 = "Second.txt";
File nfile1 = new File("Second.csv");
File nfile2 = new File("Second.txt");
File file1 = new File("TempSecond.csv");
File file2 = new File("TempSecond.txt");
FileReader reader = new FileReader(sFileName);
FileReader reader2 = new FileReader(sFileName2);
BufferedReader br1 = new BufferedReader(reader);
BufferedReader br2 = new BufferedReader(reader2);
FileWriter twriter = new FileWriter(file1);
FileWriter twriter2 = new FileWriter(file2);
BufferedWriter tbw1 = new BufferedWriter(twriter);
BufferedWriter tbw2 = new BufferedWriter(twriter2);
String line = "";
String _btitle = JOptionPane.showInputDialog (null, "Title: ", "");
while((line = br2.readLine()) !=null){
String btitle1;
if(line.contains(_btitle))
{
String conv = line.toString();
System.out.println("Search found");
btitle1 = JOptionPane.showInputDialog (null, "Replace with title: ", "");
tbw1.append(line.replaceAll("" +_btitle, "" +btitle1));
tbw1.append("\n");
tbw2.write(line.replaceAll("" +_btitle, "" +btitle1));
tbw2.newLine();
}
else {
tbw1.append(line);
tbw1.append("\n");
tbw2.write(line);
tbw2.newLine();
}
}
twriter.flush();
twriter2.flush();
tbw1.close();
tbw2.close();
br1.close();
br2.close();
nfile1.delete();
file1.renameTo(nfile1);
nfile2.delete();
file2.renameTo(nfile2);
}
catch(IOException e)
{
e.printStackTrace();
}
}
}