如何用Java编写文件列表

时间:2016-07-03 18:48:30

标签: java list file fileoutputstream

我有一个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

获取

3 个答案:

答案 0 :(得分:2)

更改 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);

答案 1 :(得分:0)

尝试接收文件路径..newfile.txt并将其作为该函数的参数。然后你可以在main或者实例化这个对象的人中放置一个for循环并将其称为n次。这有帮助吗?

答案 2 :(得分:0)

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

private static int index=0;

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

  

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

然后在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