public void saveToTextFile(String outText) throws IOException
{
BufferedWriter out = new BufferedWriter(new FileWriter(outText));
String[] keys=new String[words.size()];
int[] values=new int[words.size()];
int i=0;
for(String word : words.keySet())
{
keys[i]=word;
values[i]=words.get(word);
i++;
if(i==words.size()) //Έχουμε διασχίσει όλο το HashMap
{
break;
}
}
bubbleSort(values,keys);
for(i=0;i<words.size();i++)
{
out.write(keys[i]);
out.write(",");
out.write(values[i]);
out.write("\n");
}
out.close();
}
我有上面的功能,当我打开它创建的文件时,它就像: hello, you, are, fine, how, thanks, too, world, 以及前两个逗号后的一些奇怪的符号。就像 out.write(values[i]);和 out.write("\n");不工作!我在 bubbleSort* 之后检查了数组,它们很好!问题似乎出在写入文件中......
这是我得到的,正确的:
Word: hello Frequency: 2
Word: you Frequency: 2
Word: are Frequency: 1
Word: fine Frequency: 1
Word: how Frequency: 1
Word: thanks Frequency: 1
Word: too Frequency: 1
Word: world Frequency: 1
最佳答案
它正在执行 as documented
Writes a single character.
Parameters:
c
- int specifying a character to be written
我怀疑你想要:
out.write(String.valueOf(values[i]));
这会覆盖 Writer.write(int)
,即 slightly more clearly documented :
Writes a single character. The character to be written is contained in the 16 low-order bits of the given integer value; the 16 high-order bits are ignored.
关于java - BufferedWriter.write() 方法不会将整数写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20588900/