简体   繁体   English

无法使用 PrintWriter 创建多个文件

[英]Can't create multiple files using PrintWriter

So I'm writing a really basic program for school that first asks me to create multiple text files.所以我正在为学校编写一个非常基本的程序,它首先要求我创建多个文本文件。

PrintWriter pw = new PrintWriter( "lastname//finance.txt, lastname//taxrecords.txt" ); PrintWriter pw = new PrintWriter( "lastname//finance.txt, lastname//taxrecords.txt" );

But when I try and run the program it doesn't even create one of the files and says something is wrong with the bolded parts.但是当我尝试运行该程序时,它甚至不会创建其中一个文件,并说粗体部分有问题。 When I try and use PrintWriter twice it says the variable is already defined.当我尝试使用 PrintWriter 两次时,它表示该变量已被定义。

you need to use different instances of PrintWriter objects.您需要使用 PrintWriter 对象的不同实例。 one instance for each file you want to write in.您要写入的每个文件都有一个实例。

PrintWriter firstFileWriter = new PrintWriter("lastname//finance.txt");
PrintWriter secondFileWriter = new PrintWriter("lastname//taxrecords.txt");

then you could use firstFileWriter.write("something");那么你可以使用firstFileWriter.write("something"); to write in your first file, and secondFileWriter.write("something else");写入你的第一个文件和secondFileWriter.write("something else"); to write in second one写在第二个

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM