JAVA利用标准输入方法从键盘输入字符,并将输入的字符写到文本文件中

怎么写代码?
2024-11-18 22:48:32
推荐回答(2个)
回答(1):

import java.io.IOException;
import java.io.FileWriter;

import java.util.Scanner;

public class NewTxt {

public static void main(String[] args) throws IOException {

Scanner s = new Scanner(System.in);

System.out.println("please enter ....(Quit-退出)");

String str;
FileWriter out = new FileWriter("d:\\test.txt");
do{
str = s.nextLine();
out.write(str+"\r\n");
}while(!"Quit".equalsIgnoreCase(str));
out.close();
}
}

回答(2):

读字符想楼上就可以了

写入文件个人认为改改:

File f = new File("e:\\jsText.txt");
boolean check = true;
if(f.exists()){
System.out.println("**********Exists File************");
}else{
try{
f.createNewFile();
System.out.println("**********Creat File************");
}catch(Exception e){
System.out.println(e.getMessage());
}
}

if(check){
try{
//把数据保存到e:\jsTest.txt文件
FileWriter fw = new FileWriter("e:\\jsText.txt",true);
fw.write(s+"; \r\n");
fw.flush();
fw.close();

}catch(Exception e){System.out.println(e.getMessage());}
}

if(check){
try{
//把数据保存到e:\jsTest.txt文件
FileWriter fw = new FileWriter("e:\\jsText.txt",true);
fw.write(userNa + ";"+userAg +";"+userPa +";"+userEm +";"+userPh +";"+userSe +"; \r\n");
fw.flush();
fw.close();
System.out.println("*********-------Writer------*********");
}catch(Exception e){System.out.println(e.getMessage());}
}
}
%>




<%
if(check){
out.write("Welcome");
}else{
out.write("Sorry");
}
%>