[英]Unable to write input into a file
I have tried many times to read a user input and then write it to a file but I haven't find a solution. 我已经尝试了很多次以读取用户输入,然后将其写入文件,但是我没有找到解决方案。 Here is my code...
这是我的代码...
The main class. 主班。
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"));
}
}
This is the class that do the read and write. 这是进行读写的类。
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 + "'");
}
}
}
My problem is when I enter some text as user input, pressing enter doesn't stop me from entering inputs, but when I press Ctrl+z it ran into an infinite loop. 我的问题是当我输入一些文本作为用户输入时,按Enter并不能阻止我输入输入,但是当我按Ctrl + z时,它陷入了无限循环。
Try this one in cTedit
class. 在
cTedit
类中尝试这一方法。
bufferedReader.readLine()
will never return null
in your case. 在您的情况下,
bufferedReader.readLine()
绝不会返回null
。
Type exit
to come out of loop. 输入
exit
循环。
while((line = bufferedReader.readLine()) != null){
if(line.equals("exit")){
break;
}
fileWriter.write(line);
}
Use fileWriter.close()
in the end. 最后使用
fileWriter.close()
。
Maybe that's not the source of your problem, but I don't think that you need to check the hasnext() condition to read a line : 也许这不是问题的根源,但我认为您无需检查hasnext()条件即可读取一行:
if(scan.hasNext()){
filename = scan.nextLine();
}
just filename = scan.nextLine();
只是
filename = scan.nextLine();
should be enough. 应该足够了。
Use the following 使用以下
if(Input.equals("exit") || Input.equals("")){
System.exit(0);
}
instead of 代替
if(Input.equals("exit")){
System.exit(0);
}
In order to close the fileWriter
after just before exiting add a method closeFile
as below. 为了关闭
fileWriter
只是退出添加一个方法之前,之后closeFile
如下。
import java.util. 导入java.util。 ;
; import java.io.
导入java.io。 ;
; import java.nio.file.Files;
导入java.nio.file.Files; import java.nio.file.Path;
导入java.nio.file.Path; //import java.lang.System.*;
//导入java.lang.System。*;
public class cTedit implements ICommand{ private FileWriter fileWriter; 公共类cTedit实现ICommand {私有FileWriter fileWriter; @Override public void Execute() { // TODO Auto-generated method stub
@Override 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();
}
} }
and then callCloseFile
in cShell
class just before exiting 然后
callCloseFile
在cShell
类之前退出
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.