繁体   English   中英

Java结合两个文本文件

[英]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 (您的类运行所在的位置)中。

有关absoluterelative path更多信息,请参relative path

http://www.xyzws.com/javafaq/what-is-the-difference-between-absolute-relative-and-canonical-path-of-file-or-directory/60

除此之外,您不应该从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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM