【问题标题】:How to write file in java?如何在java中写入文件?
【发布时间】:2015-05-18 12:06:23
【问题描述】:

我面临文件写入问题,实际上当我在下面的代码中运行此代码时,while 循环会无限次迭代。

package com.demo.io;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class CopyFile {

    public static void main(String[] args) {

        FileInputStream in = null;
        FileOutputStream out = null;
        try {
            in = new FileInputStream("C:/Users/s.swain/Desktop/loginissue.txt");
            out = new FileOutputStream("C:/Users/s.swain/Desktop/output.txt");
            int c = in.read();
            while (c != -1) {
                System.out.println(c);
                out.write(c);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

任何人都可以告诉我如何编写这个文件。

谢谢

司坦苏

【问题讨论】:

  • 我建议使用 Apache Commons IO 的 IOUtils.copy(InputStream, OutputStream
  • 虽然所有答案都只是建议修复代码中的错误;我赞同托马斯的建议:除非这是为了家庭作业/学习;你不应该重新发明轮子。有一些图书馆可以为你做“文件复制”;并且很可能它们经过了更好的测试,并且可以处理许多您不会想到的“跨平台”问题。
  • Files.copy(Paths.get("pathToInputFile"), Paths.get("pathToOutputFile"));

标签: java io inputstream outputstream


【解决方案1】:

这个条件永远保持true为真,因为你永远不会在while-loop 中更新c

while (c != -1) {

使用in.read 内部 while-loop!

int c = in.read();
while (c != -1) {
    System.out.println(c);
    out.write(c);
    c = in.read();
}

【讨论】:

  • 我假设你的意思是:这个条件一旦变为真,就会永远为真。
【解决方案2】:

你错过了读取下一个字节:

int c = in.read();
while (c != -1) {
    System.out.println(c);
    out.write(c);
    c = in.read();//this line added to read next byte
}

或者,您可以简单地使用:

int c;
while (-1 != (c = in.read())) { /* condition with assignment */
    out.write(c);
}

【讨论】:

    【解决方案3】:

    你只读过一次c。 将您的 while 循环更新为

    while (c != -1) {
        System.out.println(c);
        out.write(c);
        c = in.read();
    }
    

    【讨论】:

      【解决方案4】:

      就这样做

      int c = 0;
       while ((c = in.read()) != -1) {
       System.out.println((char)c);
       out.write((byte)c);
       }
      

      【讨论】:

        【解决方案5】:

        试试这样的:

                while(c != null){
                    System.out.println(c);
                    out.write(c);
                    c = reader.readLine();
                }     
        

        【讨论】:

          【解决方案6】:
          while (c != -1) {
                          System.out.println(c);
                          out.write(c);
                      }
          

          如果 c != -1 ,您认为这里会发生什么? 你不更新c的值,所以会导致死循环。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2013-09-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多