我是java新手! 我使用此代码将数据写入.txt文件:
try {
String content = "This is the content to write into file";
File file = new File("/users/mkyong/filename.txt");
// if file doesnt exists, then create it
if ( !file.exists() ) {
file.createNewFile();
}
FileWriter fw = new FileWriter( file.getAbsoluteFile() );
BufferedWriter bw = new BufferedWriter( fw );
bw.write( content );
bw.NewLine();
bw.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
名字姓氏注册号
乔
最大
1238
我想将每个数据存储在其名称下面:
我想要这样:
FName LName Reg_Num
joe max 1238
谢谢!
答案 0 :(得分:2)
该代码应将“这是写入文件的内容”输出到文本文件中。所有这些名字来自哪里?
用于Java see this topic中的制表。
答案 1 :(得分:1)
您可以使用 \ t 在字段之间添加标签。示例代码可能类似于:
try {
String firstName = "Joe";
String lastName = "Doe";
String regNum = "123";
File file = new File("/home/parvin/Desktop/filename.txt");
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write("FirstName\t LastName\t RegNum\t \n");
bw.write(firstName + "\t" + lastName + "\t" + regNum + "\t" + "\n");
bw.newLine();
bw.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}