java接收用户输入的字符,可以使用BufferedReader 类,如下代码:
package com.qiu.lin.he;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Ceshi {
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));//新建scanner类
String in = null;
try {
System.out.println("请输入一串字符");
in = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(in);
}
}
运行结果如下:
java从键盘输入一些字符串,并将字符串保存到一个文件中!
java.io.*;
public class Test
{
public static void main(String[] args) throws IOException
{
BufferedReader buf = new BufferedReader (new InputStreamReader(System.in));
BufferedWriter buff = new BufferedWriter(new FileWriter("abc.txt"));
String str = buf.readLine();
while(!str.equals("exit"))
{
buff.write(str);
buff.newLine();
str = buf.readLine();
}
buf.close();
buff.close();
}
}