【问题标题】:How to write a list of files in Java如何在 Java 中编写文件列表
【发布时间】:2016-07-03 18:48:29
【问题描述】:

我有一个 Java 类,它使用 FileOutputStream 和 BufferedOutputStream 编写文件。这工作正常,但我的意图是我想在 java 中编写任意数量的文件,而不仅仅是一个。这是我用 Java 编写的代码

public class FileToBeTaken{


 public void fileBack(byte [] output) {

     FileOutputStream fop = null;
     BufferedOutputStream bos = null;
        File file;


        try {       
            file= new File("/Users/user/Desktop/newfile.txt");

            fop = new FileOutputStream(file);

            bos = new BufferedOutputStream(fop);

            // if file doesnt exists, then create it
            if (!file.exists()) {
                file.createNewFile();
            }



            bos.write(output);
            bos.flush();
            bos.close();

            System.out.println("Done");

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

}

这里fileBack 方法在for 循环内从另一个类调用n 次。所以每次我需要在我的桌面上写一个新文件,因为我的代码是我只取最后一次迭代的文件。另外我应该提到,对于每个迭代,作为此类的参数,发送一个字节数组,该数组由“byte [] output

【问题讨论】:

  • 你能提供一个运行示例吗?
  • 显然您已经了解可以将参数传递给函数,因为您将其用于文件的内容。我不明白是什么阻止您也将文件名作为参数传递。
  • @njzk2 因为我想在上面写的这个类中创建这些文件名所以我真的不明白如何将它作为参数传递给我的其他类

标签: java list file fileoutputstream


【解决方案1】:

改变 public void fileBack(byte [] output) {

public void fileBack(String fileName, byte [] output) {

然后通过在其中提供文件名来更改在其他类中调用方法 fileBack 的位置。即

byte output[] = //You already provide this byte array

String fileName = "/Users/user/Desktop/newfile.txt"

fileBack(fileName, output);

【讨论】:

    【解决方案2】:

    尝试获取文件路径..newfile.txt 并将其作为函数的参数。然后,您可以在main 或实例化此对象的任何人中放置一个 for 循环并调用它n 次。这有帮助吗?

    【讨论】:

    • 能否请您写下代码,因为我不明白您所说的“使其成为函数的参数”的部分
    • @alex10 ,我认为@ares 意味着您必须将方法签名从public void fileBack( byte [] output ) { 更改为此public void fileBack(String fileName, byte [] output) {,并发送newfile.txt 的路径以在fileBack 方法中使用它。跨度>
    【解决方案3】:

    只需在FileToBeTaken 类中添加一个静态和私有整数字段。

    private static int index=0;
    

    所以没有必要像你在问题 cmets 中提到的那样将文件名传递给这个类。

    因为我想在上面写的这个类中创建这些文件名

    然后在fileBack 方法中使用它,并且每次在该方法中增加一次。
    这是您的代码的更改:

    public class FileToBeTaken {
    
        // index or number of new file, also number of all written files
        // because it increments each time you call fileBack method.
        private static int index = 0;
    
        public void fileBack(byte[] output) {
    
            FileOutputStream fop = null;
            BufferedOutputStream bos = null;
            File file;
    
            try {
    
                // use user_dir to place files on desktop, 
                // even on other machines which have other names except user.
                String user_dir = System.getProperty("user.home").replace("\\", "/");
    
                // use index to create name of file.
                file = new File(user_dir+"/Desktop/newfile_" + (index++) + ".txt");
    
                fop = new FileOutputStream(file);
                bos = new BufferedOutputStream(fop);
    
                // if file doesnt exists, then create it
                if (!file.exists()) {
                    file.createNewFile();
                }
    
                bos.write(output);
                bos.flush();
                bos.close();
    
                System.out.println("Done - "+file.getName());
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (bos != null) {
                        bos.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }    
    

    如何使用:

    for (int i = 0; i < 10; i++) {
        new FileToBeTaken().fileBack("some text".getBytes());
    }
    

    输出:

    Done - newfile_0.txt
    Done - newfile_1.txt
    Done - newfile_2.txt
    Done - newfile_3.txt
    Done - newfile_4.txt
    Done - newfile_5.txt
    Done - newfile_6.txt
    Done - newfile_7.txt
    Done - newfile_8.txt
    Done - newfile_9.txt
    

    【讨论】:

    • 我不知道该怎么感谢你。这真的很有帮助。 Thnx 很多人,上帝保佑你
    • @alex10 欢迎您,如果您认为我的回答有用,请投票并与他人分享 :) ,上帝也祝福您。?
    猜你喜欢
    • 2011-11-19
    • 2013-03-23
    • 1970-01-01
    • 1970-01-01
    • 2019-10-31
    • 2020-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多