【发布时间】:2016-04-12 06:52:05
【问题描述】:
我想将数据存储在创建的文件中,但我的代码将二进制语言存储在文件中。谁能帮我存储一个字符串?
import java.io.*;
import java.util.*;
class WriteToFileExample {
public static void main(String args[]) {
try {
// Create file
FileWriter fstream = new FileWriter("out.txt");
BufferedWriter out = new BufferedWriter(fstream);
Scanner sc = new Scanner(System.in);
System.out.println("enter name");
int a = sc.nextInt();
out.write(a);
out.close();
} catch (Exception e) {
//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
【问题讨论】:
-
将其写为字符串 (write(a + "", 0, 1))。但可能有更清洁的解决方案
-
Read the Javadoc 表示您正在调用的
write方法。 -
你想要一个名字,它传统上是一个字符串,因为我们不会用数字给人们命名(我认为),然后使用@987654324 读取它@。迷人!
标签: java