java怎么把字符串写到文件里去
假如给一个字符串Strings="akjibuvuvu";怎么把这个s输出到文件a.txt...
假如给一个字符串
String s = "akjibuvuvu";
怎么把这个s输出到文件a.txt 展开
String s = "akjibuvuvu";
怎么把这个s输出到文件a.txt 展开
展开全部
String s = "akjibuvuvu";
FileWriter fw = null;
File f = new File("a.txt");
try {
if(!f.exists()){
f.createNewFile();
}
fw = new FileWriter(f);
BufferedWriter out = new BufferedWriter(fw);
out.write(s, 0, s.length()-1);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("end");
FileWriter fw = null;
File f = new File("a.txt");
try {
if(!f.exists()){
f.createNewFile();
}
fw = new FileWriter(f);
BufferedWriter out = new BufferedWriter(fw);
out.write(s, 0, s.length()-1);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("end");
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2018-06-26
展开全部
package test;
public class ceshi {
private static final String fileName = "D://data.txt";
public static void main(String[] args) throws IOException {
System.out.println("请你输入一串字符,系统自动保存到D://data.txt文件中!下面请输入你的字串:");
//进行file的初始化...
File outputFile = new File(fileName);
if(!outputFile.exists()){
outputFile.createNewFile();
}
//捕捉控制台来的字符串..
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(outputFile)), true);
String str = in.nextLine();
if (str != null && str.length() != 0) {
out.println(str);
}else{
out.println("你好,你没有在控制台输入任何字符!");
}
System.out.println("你好,请观察你的D盘下的data.txt文件,程序执行完毕.");
out.close();
in.close();
}
}
public class ceshi {
private static final String fileName = "D://data.txt";
public static void main(String[] args) throws IOException {
System.out.println("请你输入一串字符,系统自动保存到D://data.txt文件中!下面请输入你的字串:");
//进行file的初始化...
File outputFile = new File(fileName);
if(!outputFile.exists()){
outputFile.createNewFile();
}
//捕捉控制台来的字符串..
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(outputFile)), true);
String str = in.nextLine();
if (str != null && str.length() != 0) {
out.println(str);
}else{
out.println("你好,你没有在控制台输入任何字符!");
}
System.out.println("你好,请观察你的D盘下的data.txt文件,程序执行完毕.");
out.close();
in.close();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询