展开全部
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
public class TestOutPut{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String inputStr = scanner.nextLine();
File file = new File("line.txt");
FileOutputStream fileOut;
try {
fileOut = new FileOutputStream(file);
fileOut.write(inputStr.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
追问
输入完文本之后在按下回车键时要求能够显示出输入的文本
追答
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
public class TestOutPut{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String inputStr = scanner.nextLine();
System.out.println(inputStr);
File file = new File("line.txt");
FileOutputStream fileOut;
try {
fileOut = new FileOutputStream(file);
fileOut.write(inputStr.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
展开全部
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class SaveFileDemo {
public static void main(String[] args) {
saveFile("C:\\line.txt");
}
static void saveFile(String path) {
Scanner s = new Scanner(System.in);
StringBuffer sb = new StringBuffer();
while (s.hasNextLine()) {
String line = s.nextLine();
if (line.equals("exit")) break;
sb.append(line).append("\n");
}
FileWriter fw = null;
try {
File file = new File(path);
if (!file.exists()) file.createNewFile();
fw = new FileWriter(file);
fw.write(sb.toString());
fw.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fw != null) fw.close();
fw = null;
} catch (IOException e) {}
}
}
}
追问
输入完文本之后在按下回车键时要求能够显示出输入的文本
追答
在18行后加入
System.out.println(line);
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.io.*;
import java.util.*;
/**
* 输入exit用来结束程序
* @author Hardneedl
*/
public class InputDemo {
public static void main(String... args) throws IOException {
Scanner s =new Scanner(System.in);
FileWriter outputStream=new FileWriter("a.txt");
while(s.hasNextLine()){
String nextLine = s.nextLine();
if (nextLine.equals("exit"))break;
outputStream.write(nextLine+"\n");
outputStream.flush();
}
outputStream.close();
}
}
import java.util.*;
/**
* 输入exit用来结束程序
* @author Hardneedl
*/
public class InputDemo {
public static void main(String... args) throws IOException {
Scanner s =new Scanner(System.in);
FileWriter outputStream=new FileWriter("a.txt");
while(s.hasNextLine()){
String nextLine = s.nextLine();
if (nextLine.equals("exit"))break;
outputStream.write(nextLine+"\n");
outputStream.flush();
}
outputStream.close();
}
}
追问
输入完文本之后在按下回车键时要求能够显示出输入的文本
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询