【发布时间】:2016-02-25 01:27:49
【问题描述】:
我对这个作业代码有疑问。每当我运行它时,控制台不会显示任何内容,我可以编写任何我想要的内容,但脚本不会自行终止,就好像它处于无限循环中一样。更重要的是,输出文件也不写。我目前正在学习输入和输出流,所以我真的不知道我还能做些什么来解决这个问题。任何帮助将不胜感激。
public class PublicationListingProcess1 extends Publication implements Serializable{
static Publication PublicationArray[];
static String a, n, line;
static int y, c = 0, p, count = 0;
static long z;
static double s;
enum PublicationTypes{PUBLICATIONCODE, PUBLICATIONNAME, PUBLICATIONYEAR, PUBLICATIONAUTHORNAME, PUBLICATIONCOST, PUBLICATIONNBPAGES}
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
File f = new File("PublicationData_Input.txt");
Scanner input = new Scanner(f);
while(input.hasNextLine())
{
c++;
}
PublicationArray = new Publication[c];
System.out.println("Welcome to this organizing software.");
System.out.print("Please enter the destiny file for output: ");
Scanner kb = new Scanner(System.in);
File outFile = new File (kb.next()+".txt");
FileWriter output = new FileWriter (outFile);
PrintWriter writer = new PrintWriter (output);
while(input.hasNextLine())
{
String [] split = input.nextLine().split(" ");
for(int i = 0; i < split.length; i++)
{
z = Long.parseLong(split[0]);
n = split[1];
y = Integer.parseInt(split[2]);
a = split[3];
s = Double.parseDouble(split[4]);
p = Integer.parseInt(split[5]);
PublicationArray[count] = new Publication(z, n, a, y, s, p);
writer.println(PublicationArray[count]);
count++;
}
}
output.close();
input.close();
}
catch(RuntimeException e)
{
e.getMessage();
}
catch (FileNotFoundException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
【问题讨论】:
-
你永远不会关闭
PrintWriter -
你正在阅读一个空白文本文件..你的程序没有什么要解析的
-
尝试传入一个包含 1 或 2 行示例的文本文件,看看您会遇到什么错误(如果有)
-
The file on its own it's a file provided by the teacher, which is read in the console: 900876512 Core_Java 2007 Mike_Simon 129.99 568 765867999 Java_Applications_for_Programmers 2010 David_Wilson_and_Jack_Westman 173.25 672 465979798 From_Java_to_C++ 2008 Linda_Jackson 118.73 439 760098908 Microsoft_VC++ 2006 Garry_Wesley 165.20 416 529086890 Software_Engineering 2005 Alain_Macmillan 219.99 651 765867999 Visual_Basic 2004 Mary_Rosen 108.33 388 但根本没有转移
-
我还添加了以下几行: input.close();输出.flush();输出.close();但它仍然没有将任何内容写入文件。感谢您顺便回答。
标签: java arrays object file-io outputstream