从Java中的文件生成的数组中删除文本

从Java中的文件生成的数组中删除文本,java,arrays,list,text-files,file-writing,Java,Arrays,List,Text Files,File Writing,我对Java比较熟悉,对于一个个人项目,我正在尝试编写一个程序,该程序可以接受用户输入,写入一个文本文件,并从一个从同一文本文件构建的数组中删除一行(由数组的索引指示)中的文本,这样,当您打印出文件的内容时,您删除的内容实际上是删除的 这是我的代码,对于我看不到的任何其他错误,我深表歉意: public class Writing { Scanner input = new Scanner(System.in); File customerFile = new File("Cus

我对Java比较熟悉,对于一个个人项目,我正在尝试编写一个程序,该程序可以接受用户输入,写入一个文本文件,并从一个从同一文本文件构建的数组中删除一行(由数组的索引指示)中的文本,这样,当您打印出文件的内容时,您删除的内容实际上是删除的

这是我的代码,对于我看不到的任何其他错误,我深表歉意:

public class Writing {
    Scanner input = new Scanner(System.in);
    File customerFile = new File("Customers.txt");
    String[] fileArray;

public Writing() throws IOException{
    FileReader fileReader = new FileReader(customerFile);
    BufferedReader bufferedReader = new BufferedReader(fileReader);
    List<String> lines = new ArrayList<String>();
    String line = null;

    while ((line = bufferedReader.readLine()) != null) {
        lines.add(line);
    }
    bufferedReader.close();
    fileArray = lines.toArray(new String[lines.size()]);
}
public void FileWrite() throws IOException {

        String inputData = input.nextLine();
        if (!customerFile.exists()) {
            customerFile.createNewFile();

        }
        FileWriter fileWriter = new FileWriter(customerFile.getName(),true);
        BufferedWriter bufferWriter = new BufferedWriter(fileWriter);
        bufferWriter.write(inputData + "\n");
        bufferWriter.close();

        System.out.println("Done.");
}

//Here is the one I have trouble with
public void RemoveFile() throws IOException {
    LinkedList<String> cusList = new LinkedList<String>(Arrays.asList(fileArray));

    System.out.println("Which one would you like to delete? [0-#].");
    //then it prints the list so you can see what you want to delete

        for(String x: cusList){
            System.out.println(x);
        }

            String removeThis = input.nextLine();
            int removeNum = Integer.parseInt(removeThis);
            if()){ //if the user input contains the number that occurs as an index in the list
                //not sure what to put here
                    fileArray = cusList.toArray(fileArray);

            }else{
                System.out.println("Remove failed.");
            }
}
}
方法2失败:

   //attempts to remove the content of the inputted index within the list 

if(removeNum.equals(cusList.indexOf(removeNum))){
    cusList.remove(removeNum);
    cusList.toArray(fileArray);
}

//else statement would go here
救命啊

解决方案: 多亏了所有提供帮助的人,我才明白了这一点。我意识到一个更简单的解决方案是清除文本文件的所有内容,使用一个包含所有内容的数组,然后在所有更改之后将这些内容写回文本文件。这是我的密码:

   // RemoveFile method
    public void RemoveFile() throws IOException {
        LinkedList<String> cusList = new LinkedList<String>(
            Arrays.asList(fileArray));

    System.out.println("Which one would you like to delete? Each customer is given a number [1-#].");
    System.out.println("The file will change when program terminated.");
    for (int i = 1; i < fileArray.length; i++) {
        for (String x : cusList) {
            System.out.println("Customer " + i + ": " + x);
            i++;
        }
    }

    String removeThis = input.nextLine();
    int removedNum = Integer.parseInt(removeThis);
    int removeNum = removedNum - 1;
    if (removeNum >= 0 && removeNum < cusList.size()) {
        cusList.remove(removeNum);
        fileArray = cusList.toArray(fileArray);

        PrintWriter writer = new PrintWriter(customerFile);
        writer.print("");// clears the file contents
        writer.close();

        FileWriter fileWriter = new FileWriter(customerFile.getName(), true);
        BufferedWriter bufferWriter = new BufferedWriter(fileWriter);

        for (String x : cusList) {
            bufferWriter.write(x + "\n");

        }
        bufferWriter.close();
    } else {
        System.out.println("Index out of bounds, " + removeNum + " >= "
                + cusList.size());
    }
//RemoveFile方法
public void RemoveFile()引发IOException{
LinkedList cusList=新建LinkedList(
asList(fileArray));
System.out.println(“您想删除哪一个?给每个客户一个数字[1-#]”;
System.out.println(“当程序终止时,文件将更改”);
for(int i=1;i=0&&removeNum=”
+cusList.size());
}

这似乎工作得很好,程序员的建议使逻辑工作,而不是不断返回false,然后允许程序将数据从文件提取到数组中,清除数组的内容,然后将其写回文件。

custList
包含一个
字符串
值列表从文件中,
contains
将始终返回
false
,因为它正在寻找匹配中的相等值(
1!=“一些字符串值”

如果(removeNum>=0&&removeNum,则需要执行类似于
的操作,这样您就知道该值在
列表
的范围内,然后只需使用
removeNum
删除给定索引中的值,
custList.remove(removeNum)

if(removeNum>=0&&removeNum=“+custList.size());
}

custList
包含文件中的
String
值列表,
contains
将始终返回
false
,因为它在匹配中寻找相等值(
1!=“某些字符串值”

如果(removeNum>=0&&removeNum
,则需要执行类似于
的操作,这样您就知道该值在
列表
的范围内,然后只需使用
removeNum
删除给定索引中的值,
custList.remove(removeNum)

if(removeNum>=0&&removeNum=“+custList.size());
}
文件填充=新文件(“input.dat”);
File outFile=新文件(“output.dat”);
扫描仪扫描=新扫描仪(填充);
PrintWriter out=新的PrintWriter(输出文件);
while(scan.hasNextLine()){
String line=scan.nextLine();
if(){
out.println(行);
}
}
scan.close();
out.close();
原则上,您不应试图编辑作为输入接收的文件,而应使用更改的输出编写新文件。如果确实需要新文件与旧文件同名,请在两个文件上都执行file.renameTo()。

file infle=new file(“input.dat”);
File outFile=新文件(“output.dat”);
扫描仪扫描=新扫描仪(填充);
PrintWriter out=新的PrintWriter(输出文件);
while(scan.hasNextLine()){
String line=scan.nextLine();
if(){
out.println(行);
}
}
scan.close();
out.close();

原则上,您不应尝试编辑作为输入接收的文件,而应使用更改的输出编写新文件。如果确实需要新文件与旧文件同名,请执行file.renameTo()在这两个文件上。

正如您所写,在发布此问题之前,您自己做了几次尝试。您能具体说明一下这些吗?到目前为止,您在if条件中尝试了什么,以及您是如何尝试删除该行的?如果我们知道您正在努力解决的问题,而不仅仅是给出答案,您的问题将更有价值这很有效。好的,我做了编辑。对不起,问这类问题也很新鲜。正如你所写的,你在发布这个问题之前自己做了几次尝试。你能具体说明一下这些吗?到目前为止,你在if条件中尝试了什么,你是如何尝试删除这行的?如果我们知道你在挣扎的是什么,而不是仅仅给出一个有效的答案。好吧,我做了编辑。对不起,对于问这些类型的问题,我也是一个新手。这让陈述有效(非常感谢!),但它似乎不会改变文件的内容。不过,现在我是其中之一
   // RemoveFile method
    public void RemoveFile() throws IOException {
        LinkedList<String> cusList = new LinkedList<String>(
            Arrays.asList(fileArray));

    System.out.println("Which one would you like to delete? Each customer is given a number [1-#].");
    System.out.println("The file will change when program terminated.");
    for (int i = 1; i < fileArray.length; i++) {
        for (String x : cusList) {
            System.out.println("Customer " + i + ": " + x);
            i++;
        }
    }

    String removeThis = input.nextLine();
    int removedNum = Integer.parseInt(removeThis);
    int removeNum = removedNum - 1;
    if (removeNum >= 0 && removeNum < cusList.size()) {
        cusList.remove(removeNum);
        fileArray = cusList.toArray(fileArray);

        PrintWriter writer = new PrintWriter(customerFile);
        writer.print("");// clears the file contents
        writer.close();

        FileWriter fileWriter = new FileWriter(customerFile.getName(), true);
        BufferedWriter bufferWriter = new BufferedWriter(fileWriter);

        for (String x : cusList) {
            bufferWriter.write(x + "\n");

        }
        bufferWriter.close();
    } else {
        System.out.println("Index out of bounds, " + removeNum + " >= "
                + cusList.size());
    }
if (removeNum >= 0 && removeNum < cusList.size()) { 
    custList.remove(removeNum);
}else{
    System.out.println("Index out of bounds, " + removeNum + " >= " + custList.size());
}
File inFile = new File("input.dat");
File outFile = new File("output.dat");
Scanner scan = new Scanner(inFile);
PrintWriter out = new PrintWriter(outFile);
while(scan.hasNextLine()) {
    String line = scan.nextLine();
    if(<criterion_to_determine_whether_to_keep_line_or_not>) {
        out.println(line);
    }
}
scan.close();
out.close();