如何保存字符串和用户输入的字符串?下面给出错误。 我该如何解决。可能存在更多错误。 什么是“未封闭的字符串文字”?在Java中。
//Scanner sc = new Scanner(System.in);
//String userInput = sc.nextLine();
//String combining = "hey"+userInput;
//System.out.println(combining);
import java.io.*;
import java.util.*;
public class FileWrite
{
public static void main(String[] args)
{
String input = null, file, fileName = null;
try(BufferedReader br = new BufferedReader(new InputStreamReader(System.in)))
{
System.out.println("Enter the file name you wish to write the data");
System.out.println("==============================================");
Scanner sc = new Scanner(System.in);
fileName = sc.nextLine(); //accepting the name of the file
file = "C:\"+fileName+".txt"; //error line
file.readLine(); //will this work?
System.out.println("Enter your data");
System.out.println("==============================================");
System.out.println("");
System.out.println("");
do
{
input = br.readLine(); //accepting the data to write in the file
if(input.toLowerCase().equals("end")) //end to terminate the program
{
break;
}
else
{
try(BufferedWriter bw = new BufferedWriter(new FileWriter(file, true)))
{
bw.write(input);
bw.newLine();
}
catch(IOException e)
{
System.out.println(e);
System.exit(0);
}
}
}while(input != null);
}catch(Exception ex)
{
System.out.println(ex);
System.exit(0);
}
}
}
答案 0 :(得分:0)
它表示未封闭的字符串文字。 可能还有更多错误。
mv
答案 1 :(得分:0)
我认为以下代码将帮助您将许多字符串写入新文件。
import java.io.*;
//we did not use the classes of java.util package so let me remove it;
public class FileWrite1 {
public static void main(String[] args) {
//we will use file name in File class so let me remove String file
String input = null, fileName = null;
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
System.out.println("Enter the file name you wish to write the data");
System.out.println("==============================================");
//Scanner class does not need because you have already use BufferedReader class
//Scanner sc = new Scanner(System.in);
//So, if you have BufferedReader class, let's use it below
fileName = br.readLine(); //accepting the name of the file
//if you want to create a new file using the name "fileName", you should use the class File.
File file = new File("D:\\" + fileName + ".txt");
//code in below is not necessary because of File class
//file = "C:\"+fileName+".txt"; //error line
//code in below will not work, because String file is not from BufferedReader class on your code, so let me take it to comment
//file.readLine(); //will this work?
System.out.println("Enter your data");
System.out.println("==============================================");
System.out.println("");
System.out.println("");
do {
input = br.readLine(); //accepting the data to write in the file
if (input.toLowerCase().equals("end")) { //end to terminate the program
break;
} else {
// inside FileWriter object, you should use the object name of File Class, not String file
try (BufferedWriter bw = new BufferedWriter(new FileWriter(file, true))) {
bw.write(input);
bw.newLine();
} catch (IOException e) {
System.out.println(e);
System.exit(0);
}
}
} while (input != null);
} catch (Exception ex) {
System.out.println(ex);
System.exit(0);
}
}
}
让我解释一下。
BufferedReader
类,则可以使用它来获取
输入,因此我们不应在您的代码中包含java.util
包的类。String file
不需要。因为,我们将使用file
File
类中的名称。因此,让我们将其保留在未来。fileName = br.readLine();
如果要将输入内容写入新文件,则应调用该类
File
这样:
File file = new File("D:\\" + fileName + ".txt");
D:\是驱动器名称,fileName是在D上新创建的文件的名称 drive和.txt是文件的扩展名。
file.readLine();将不起作用,因为在您的代码中,字符串文件为 不是来自BufferedReader类。
在FileWriter对象中,您应该使用File的对象名称 类,而不是字符串文件
在您的IDE上运行此代码。不要忘记更改File类上的文件位置,在我的情况下,它位于D:\驱动器中。