java中怎么输入一串字符,要包含的头文件等都麻烦写下?
展开全部
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();
}
}
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();
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询