会JAVA编程请进!!!写一个运行程序,将用户从键盘上输入的十行文字存入文件。
1个回答
展开全部
Java源程序:
import java.io.*;
import java.util.*;
public class yurenjie {
public static void main(String args[]){
try{
Scanner scan = new Scanner(System.in);
StringBuffer buffer = new StringBuffer(); //字符串缓冲区
String temp;
System.out.println("请输入10行字符:");
for(int i=0; i<10; i++){ //将10个字符串预先存入字符串缓冲区
temp = scan.next();
buffer.append(temp + "\n");
}
String fileName = "d:\\data.txt"; //保存位置
OutputStream os = new FileOutputStream(fileName);
PrintStream ps = new PrintStream(os);
ps.print(buffer.toString()); //写入文件
ps.close();
os.close();
}
catch(Exception ex){
System.out.println("系统I/O错误!");
}
}
}
import java.io.*;
import java.util.*;
public class yurenjie {
public static void main(String args[]){
try{
Scanner scan = new Scanner(System.in);
StringBuffer buffer = new StringBuffer(); //字符串缓冲区
String temp;
System.out.println("请输入10行字符:");
for(int i=0; i<10; i++){ //将10个字符串预先存入字符串缓冲区
temp = scan.next();
buffer.append(temp + "\n");
}
String fileName = "d:\\data.txt"; //保存位置
OutputStream os = new FileOutputStream(fileName);
PrintStream ps = new PrintStream(os);
ps.print(buffer.toString()); //写入文件
ps.close();
os.close();
}
catch(Exception ex){
System.out.println("系统I/O错误!");
}
}
}
追问
怎么文字没有存到文件里?
追答
写进入了。你在D盘找一下data.txt文件就知道了。
import java.io.*;
import java.util.*;
public class yurenjie {
public static void main(String args[]){
try{
Scanner scan = new Scanner(System.in);
StringBuffer buffer = new StringBuffer(); //字符串缓冲区
String temp;
System.out.println("请输入10行字符:");
for(int i=0; i<10; i++){ //将10个字符串预先存入字符串缓冲区
temp = scan.next();
buffer.append(temp + "\n");
}
String fileName = "d:\\data.txt"; //保存位置
OutputStream os = new FileOutputStream(fileName);
PrintStream ps = new PrintStream(os);
ps.print(buffer.toString()); //写入文件
ps.flush();
ps.close();
os.close();
}
catch(Exception ex){
System.out.println("系统I/O错误!");
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询