[英]Java combining two text files
我的Java類有一個作業,要求我合並兩個文本文件。
這是我到目前為止的代碼。
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
public class CombineTwoFile {
public static void main(String[] args) throws IOException
{
ArrayList<String> list = new ArrayList<String>();
try
{
BufferedReader br = new BufferedReader(new FileReader( "A.txt"));
BufferedReader r = new BufferedReader(new FileReader( "B.txt"));
String s1 =null;
String s2 = null;
while ((s1 = br.readLine()) != null)
{
list.add(s1);
}
while((s2 = r.readLine()) != null)
{
list.add(s2);
}
}
catch (IOException e)
{
e.printStackTrace();
}
BufferedWriter writer=null;
writer = new BufferedWriter(new FileWriter("B.txt"));
String listWord;
for (int i = 0; i< list.size(); i++)
{
listWord = list.get(i);
writer.write(listWord);
writer.write("\n");
}
System.out.println("completed");
writer.close();
}
}
現在,當我編譯它時,我收到此消息。
java.io.FileNotFoundException: A.txt (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)
at java.io.FileInputStream.<init>(FileInputStream.java:79)
at java.io.FileReader.<init>(FileReader.java:41)
at CombineTwoFile.main(CombineTwoFile.java:15)
completed
我在Apple計算機上使用Coderunner,我認為也許將文本文件編寫為“ C:/Users/dell/Desktop/Test/input1.txt”可能會解決此問題,但是我不確定如何編寫對應的文本文件我的硬盤。 感謝您的關注,我非常感謝您的幫助。
這里有幾處可以改進的地方。
首先,正如其他人指出的那樣,您需要指定文件的正確路徑,其執行方式假定文件位於類路徑中,而顯然不在。
您可以指定絕對路徑,也可以指定相對於類的相對路徑。
如果您想保持原樣,則需要將文件放在classpath
(您的類運行所在的位置)中。
有關absolute
和relative path
更多信息,請參relative path
:
除此之外,您不應該從main
拋出Exception,而應該處理它。
我還建議您使用新的try-with-resources
。
例:
try(BufferedReader bf = new BufferedReader(new FileReader( "C:\\Users\\...\\A.txt"));){
//do something
} catch(IOException e){
//handle
}
//no need to close the streams, the jgc will handle that for you
當您使用完流后,它將在try塊內為您關閉流。
如果您的老師(如您在一個注釋中添加的)希望您能夠動態選擇路徑,則需要從控制台輸入它並將其用作絕對路徑。
Scanner s = new Scanner(System.in);
String path = s.readLine(); //use this as absolute path
如果您需要通過GUI
,則需要一個JFileChooser
。
就寫作而言,同樣的建議適用。
您還可以避免通過使用PrintWriter來寫行+'\\ n'。 它將提供一個println(String s)方法,自動刷新,並且出於可移植性的原因而更好。
作為一個小紙條,在這種情況下,你實際上並不需要s2
,采用s1
再次會做就好了。
您的Java程序無法在所需位置找到“ A.txt”,要知道將文件放置在何處,可以使用system.getproperty( user.dir )
來了解系統在哪里尋找文件。 另一種方法是您可以在new File('c:\\\\something\\\\A.txt');
寫入絕對路徑new File('c:\\\\something\\\\A.txt');
希望能幫助到你
盧卡斯,您的程序絕對正確。 您不需要更正任何內容,只需手動創建文件“ A.txt”,然后再次運行此代碼即可。
只是地方
System.out.println(new File("A.txt").getCanonicalPath());
之前
BufferedReader br = new BufferedReader(new FileReader( "A.txt"));
BufferedReader r = new BufferedReader(new FileReader( "B.txt"));
您將獲得異常信息之前的確切路徑,例如:
C:\Users\PiyushMittal\Downloads\Java-mongodb-hello-world-example\mongodb\A.txt
java.io.FileNotFoundException: A.txt (The system cannot find the file specified)
completed at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at java.io.FileReader.<init>(FileReader.java:58)
at com.mkyong.core.CombineTwoFile.main(CombineTwoFile.java:19)
第一行是您必須放置文件的地方:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.