我已多次尝试读取用户输入,然后将其写入文件但我找不到解决方案。这是我的代码......
主要班级。
import java.util.HashMap;
import java.util.Scanner;
public class cShell{
static String Currentpath="C:\\";
public String Current = Currentpath;
static HashMap<String, ICommand> myhashData=new HashMap<String, ICommand>();
public static void main(String[] args)
{
myhashData.put("ltf", new cITF());
myhashData.put("nbc", new cNBC());
myhashData.put("gdb", new cGDB());
myhashData.put("Tedit", new cTedit());
String Input = "";
do{
System.out.print(Currentpath+"> ");
//String Input = "";
Scanner scan = new Scanner(System.in);
if(scan.hasNext()){
Input = scan.nextLine().trim();
}
if(Input.equals("exit")){
System.exit(0);
}
if(myhashData.containsKey(Input)){
ICommand myCommand=myhashData.get(Input);
myCommand.Execute();
}
else{
System.out.println("Invalid Command");
}
}while(!Input.equals("exit"));
}
}
这是进行读写的类。
import java.util.*;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
//import java.lang.System.*;
public class cTedit implements ICommand{
@Override
public void Execute() {
String filename = "";
System.out.println("Enter the file name to be edited");
Scanner scan = new Scanner(System.in);
if(scan.hasNext()){
filename = scan.nextLine();
}
String line = null;
try{
InputStreamReader ISR = new InputStreamReader(System.in);
//FileReader fileReader = new FileReader(ISR);
FileWriter fileWriter = new FileWriter(cShell.Currentpath+"\\"+filename);
BufferedReader bufferedReader = new BufferedReader(ISR);
System.out.println("Enter text into the file");
while((line = bufferedReader.readLine()) != null){
fileWriter.write(line);
}
bufferedReader.close();
}catch(FileNotFoundException ex){
System.out.println("Unable to open file '" + filename + "'");
}
catch(IOException ex){
System.out.println("Error reading file '" + filename + "'");
}
}
}
我的问题是当我输入一些文本作为用户输入时,按Enter键不会阻止我输入输入,但是当我按下Ctrl + z时它会进入一个无限循环。
答案 0 :(得分:0)
在cTedit
课程中试试这个。
bufferedReader.readLine()
永远不会在您的案例中返回null
。
键入exit
以退出循环。
while((line = bufferedReader.readLine()) != null){
if(line.equals("exit")){
break;
}
fileWriter.write(line);
}
最后使用fileWriter.close()
。
答案 1 :(得分:0)
也许这不是问题的根源,但我认为你不需要检查hasnext()条件来读取一行:
if(scan.hasNext()){
filename = scan.nextLine();
}
只需filename = scan.nextLine();
即可。
答案 2 :(得分:0)
使用以下
if(Input.equals("exit") || Input.equals("")){
System.exit(0);
}
而不是
if(Input.equals("exit")){
System.exit(0);
}
要在退出之前关闭fileWriter
,请按以下方式添加方法closeFile
。
import java.util。; import java.io。; import java.nio.file.Files; import java.nio.file.Path; // import java.lang.System。*;
public class cTedit实现ICommand { private FileWriter fileWriter; @覆盖 public void Execute(){ // TODO自动生成的方法存根
String filename = "";
System.out.println("Enter the file name to be edited");
Scanner scan = new Scanner(System.in);
if(scan.hasNext()){
filename = scan.nextLine();
}
String line = null;
try{
InputStreamReader ISR = new InputStreamReader(System.in);
//FileReader fileReader = new FileReader(ISR);
fileWriter = new FileWriter(cShell.Currentpath+"\\"+filename);
BufferedReader bufferedReader = new BufferedReader(ISR);
System.out.println("Enter text into the file");
while((line = bufferedReader.readLine()) != null){
fileWriter.write(line);
}
bufferedReader.close();
}catch(FileNotFoundException ex){
System.out.println("Unable to open file '" + filename + "'");
}
catch(IOException ex){
System.out.println("Error reading file '" + filename + "'");
} finally {
fileWriter.close();
}
}
public void closeFile() {
fileWriter.close();
}
}
在退出之前,在callCloseFile
课程中然后cShell