【发布时间】:2015-03-16 05:53:05
【问题描述】:
class Start
{
File plikIN;
Scanner in;
PrintWriter out;
Start(String input, String output)
{
plikIN = new File(input);
try{
in = new Scanner(plikIN);
out = new PrintWriter(output);
} catch (FileNotFoundException e) {System.out.println("Nie odnaleziono podanego pliku\n"+e);}
}
private void saveing() throws IOException
{
String word;
int wordLength;
String wordTable[];
char c;
while((word = in.next()) != null)
{
wordLength = word.length();
wordTable = new String[wordLength];
for(int k=0; k<wordTable.length; ++k)
{
c = word.charAt(k);
out.println(c);
}
}
out.close();
}
public static void main(String[] args) throws IOException
{
String nazwaPlikuWejsciowego = args[0];
String nazwaPlikuWyjsciowego = args[1];
Start doit = new Start(nazwaPlikuWejsciowego, nazwaPlikuWyjsciowego);
doit.saveing();
}
}
我的问题是保存到文件。在上面的saveing 方法之后,文件不包含任何单个字符。例如,当我将out.close() 移动到while 时,该文件包含一个单词。当out.close() 在for 中时,程序只保存一个字符。为什么?
【问题讨论】:
-
你的循环
while((word = in.next())会报错,看我的回答。
标签: java file input output printwriter