JAVA 如何将 int 写入文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20184852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JAVA How to write an int into a file?
提问by MexicanJudge
I am supposed to create a file (done, it's called "factoriales.txt") and print in it the value of 10! (which is 3628800), the thing is, I can't seem to write the value into a file. I already know how to write text, but this wont work.... here's what I have so far. Please help!
我应该创建一个文件(完成,它被称为“factoriales.txt”)并在其中打印值 10!(这是 3628800),问题是,我似乎无法将该值写入文件。我已经知道如何写文本,但这行不通......这是我目前所拥有的。请帮忙!
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class D09T9e2
{
public static void main(String[] args)
{
try
{
File fac = new File("factoriales.txt");
if (!fac.exists())
{
fac.createNewFile();
}
System.out.println("The file has been created.");
int r = 1;
for (int i = 1; i<=10; i--)
{
r = r * i;
}
FileWriter write = new FileWriter(fac);
write.write(""+r);
write.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
Here's how I solved it thanks to everybody in here, I hope you like this simple method:
感谢这里的所有人,这是我解决它的方法,我希望你喜欢这个简单的方法:
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class D09T9e2
{
public static void main(String[] args)
{
try
{
File fac = new File("factoriales.txt");
if (!fac.exists())
{
fac.createNewFile();
}
System.out.println("\n----------------------------------");
System.out.println("The file has been created.");
System.out.println("------------------------------------");
int r = 1;
FileWriter wr = new FileWriter(fac);
for (int i = 1; i<=10; i++)
{
r = r * i;
wr.write(r+System.getProperty( "line.separator" ));
}
wr.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
回答by ManZzup
the reason any way is that
任何方式的原因是
for (int i = 1; i<=10; i--)
runs to infinity since 1 cannot be reduced to 10
运行到无穷大,因为 1 不能减少到 10
it should be
它应该是
for (int i = 1; i<=10; i++)
and change data type to long as well
并将数据类型也更改为 long
回答by MouseLearnJava
Change the for-loop:
更改 for 循环:
From
从
for (int i = 1; i<=10; i--)
to
到
for (int i = 1; i<=10; i++)
and you will get the right result.
你会得到正确的结果。
have a try and go ahead.
试一试,继续前进。
回答by Evgeniy Dorofeev
If you want to save a number in text format use PrintWriter, it has methods for writing numbers.
如果你想以文本格式保存数字,使用 PrintWriter,它有写数字的方法。
回答by user3020494
public static void main(String[] args)
{
try
{
File fac = new File("c:\bea\factoriales.txt");
if (!fac.exists())
{
fac.createNewFile();
}
System.out.println("The file has been created.");
int r = 1;
FileWriter write = new FileWriter(fac);
write.write("");
for (int i = 1; i<=10; i++)
{
r = r * i;
write.append(r + "\n");
}
write.flush();
write.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
Output:
输出:
1
2
6
24
120
720
5040
40320
362880
3628800