简体   繁体   English

从一个Java文件读取并写入另一个Java文件

[英]Reading from one java file and writing to another one

I want content of one .java file to the another .java file. 我想要一个.java文件的内容到另一个.java文件。 I am doing this with FileInput/FileOutput Stream classes in eclipse IDE . 我正在使用Eclipse IDE中的 FileInput/FileOutput Stream类进行此操作。 I have put one file named FileToFile.java inside Nisarg/src/FiliIO(package) . 我在Nisarg / src / FiliIO(package)中放了一个名为FileToFile.java的文件。

And I am getting FileNotFoundException at line 12. I want to know why this exception raised? 而且我在第12行收到FileNotFoundException 。我想知道为什么会引发此异常?

This is what I actually got at runtime.. 这是我在运行时实际得到的。

Exception in thread "main" java.io.FileNotFoundException: FileToFile.java (The system cannot find the file specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at FileIO.FileToFile.main(FileToFile.java:12)

This is a piece of code: 这是一段代码:

 package FileIO;

 import java.io.*;
 public class FileToFile {

    /**
     * @param args
     */
    public static void main(String[] args)throws IOException {
        // TODO Auto-generated method stub

            FileInputStream i=new FileInputStream("FileToFile.java");// Current file content is wanted to be written...
            FileOutputStream o=new FileOutputStream("M.java"); // Destination file which is also in same place.(Nisarg/src/FileIO(package)...
            int a=0;
            while((a=i.read())!=-1)
            {
                o.write((byte)a);
            }
            o.close();
            i.close();
            System.out.print("Done");

    }
}

What should be done to achieve my requirements? 要达到我的要求应该做什么? I have searched but I was unable to where to put the file. 我已经搜索过,但是无法将文件放在哪里。 Thank you in advance..!! 先感谢您..!!

Java cant find your input file FileToFile.java, that's what is basically mean. Java找不到您的输入文件FileToFile.java,这基本上是什么意思。 You either can specify a absolute file path or find out what folder your main class is located at and place your file FileToFile.java there. 您可以指定一个绝对文件路径,也可以找出您的主类位于哪个文件夹,然后将文件FileToFile.java放在此处。

Find the current directory of your main class, use System.getProperty("user.dir") 找到您的主类的当前目录,使用System.getProperty(“ user.dir”)

您必须进行更改“ src \\ FileIO \\ FileToFile.java”

package fileIO;

import java.io.*;

public class FileToFile {


   public static void main(String[] args)throws IOException {


 FileInputStream i=new FileInputStream("src\\fileIO\\FileToFile.java");

           FileOutputStream o=new FileOutputStream("F:\\M.java"); 

           int a=0;
           while((a=i.read())!=-1)
           {
               o.write((byte)a);
           }
           o.close();
           i.close();
           System.out.print("Done");

   }
}

You should have to use 1)the absolute path for your file 2)Find the current directory using System.getProperty("user.dir") and place your file there. 您应该使用1)文件的绝对路径2)使用System.getProperty(“ user.dir”)查找当前目录,然后将文件放在此处。 3)You can change your current working directory for your application by Go to run configuration >> Arguments >> Other radio button. 3)您可以通过转到运行配置>>参数>>其他单选按钮来更改应用程序的当前工作目录。 Then enter an absolute path name as the working directory for the launched application.Place your file in the specified directory. 然后输入绝对路径名称作为已启动应用程序的工作目录。将文件放置在指定目录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 文件I / O:从一个文件读取并写入另一个文件(Java) - File I/O: Reading from one file and writing to another (Java) Java NIO从一个套接字读取并写入另一个套接字的固有缺陷? - Java NIO inherent flaw in reading from one socket and writing to another? 读取一个文件并写入另一个文件失败 - Reading in one file and writing out to another fails 2个Java进程-一种读取和一种写入同一文件 - 2 java processes - one reading and one writing to the same file 使用FileInputStream从一个文件读取并使用FileOutputStream写入另一个文件 - Reading from one file using FileInputStream and writing to another file using FileOutputStream 读取文件与写入文件 - Reading a File versus Writing One 从一个文件读取,导出到另一个文件 - Reading from one file, export to another 从一个TextArea(在一个.fxml中)读取,并写入另一个TextArea(在另一个.fxml中)。 不嵌套的控制器 - Reading from one TextArea (in one .fxml) and writing to another TextArea (in another .fxml). Not nested controllers 从一个类而不是另一个类读取 Java 中的属性文件时没有这样的文件或目录 - No such file or directory when reading Properties File in Java from one Class but not another Java,NIO,具有读写功能的一个线程 - Java, NIO, one thread with reading and writing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM