如何用Java编写文件列表
我有一个Java类,它使用FileOutputStream和BufferedOutputStream编写文件。这很好,但我的目的是希望用java编写任意数量的文件,而不仅仅是一个。这是我用Java编写的代码如何用Java编写文件列表,java,list,file,fileoutputstream,Java,List,File,Fileoutputstream,我有一个Java类,它使用FileOutputStream和BufferedOutputStream编写文件。这很好,但我的目的是希望用java编写任意数量的文件,而不仅仅是一个。这是我用Java编写的代码 public class FileToBeTaken{ public void fileBack(byte [] output) { FileOutputStream fop = null; BufferedOutputStream bos = null;
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[]输出< < /p> 尝试将文件路径<代码> .NexField.txt>代码>并将其转换为函数的参数。然后可以在<代码>主< <代码>中放置for循环,或者任何实例化该对象并调用它的代码<代码> n>代码>倍。这有帮助吗?< /p> 尝试在文件路径<代码>…NexField.txt < /代码>中使之成为一个PAR。函数的参数。然后您可以在main
或实例化此对象的任何人中放置for循环,并调用它n
次。这有帮助吗?更改
public void fileBack(字节[]输出){
到
public void fileBack(字符串文件名,字节[]输出){
然后通过在另一个类中提供文件名来更改调用方法fileBack的位置
byte output[] = //You already provide this byte array
String fileName = "/Users/user/Desktop/newfile.txt"
fileBack(fileName, output);
改变
public void fileBack(字节[]输出){
到
public void fileBack(字符串文件名,字节[]输出){
然后通过在另一个类中提供文件名来更改调用方法fileBack的位置
byte output[] = //You already provide this byte array
String fileName = "/Users/user/Desktop/newfile.txt"
fileBack(fileName, output);
只需在
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());
}
只需在
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());
}
您能提供一个runnale示例吗?很明显,您已经了解可以将参数传递给函数,因为您将参数用于文件的内容。我不明白是什么阻止您将文件名作为参数传递。@njzk2因为我想在我上面写的这个类中创建这些文件名,所以我不打算rstand如何将其作为参数传递给我的其他类?请提供一个runnale示例好吗?显然,您已经了解可以将参数传递给函数,因为您将其用于文件内容。我不明白是什么阻止您将文件名作为参数传递。@njzk2,因为我想创建这些文件名s在我上面写的这个类中,所以我真的不明白如何将它作为参数传递给我的其他类。你能写下代码吗?因为我不明白你说的“将它变成函数的参数”的部分@alex10,我认为@ares意味着你必须将方法签名从
public void fileBack(字节[])更改为输出){
到这个public void fileBack(String fileName,byte[]output){
,并发送newfile.txt
的路径以在fileBack
方法中使用它。你能写下代码吗,因为我不理解你说的“将它变成函数参数”的部分“@alex10,我认为@ares意味着您必须将方法签名从public void fileBack(byte[]output){
更改为thispublic void fileBack(String fileName,byte[]output){
,并发送newfile.txt
的路径以在fileBack
方法中使用它。我不知道如何感谢你。它真的很有帮助。感谢很多人,上帝保佑you@alex10欢迎你,如果你认为我的答案有用,就投票吧,并与其他人分享:),上帝也保佑你。我不知道如何感谢你。真的是他有很多人,愿上帝保佑you@alex10欢迎你,如果你认为我的答案很有用,就投票吧,并与其他人分享:),上帝也保佑你。