简体   繁体   中英

I cannot write to my .CSV file

My guess is, the code I've written doesn't work with .CSV files, but only .txt.

The purpose of my code is to take the user input from field1, and check against my .CSV file to see if there is an instance of the user input located within the file. If there is, then it will be replaced by the user input from field2.

This works with my .txt file, but not with my .CSV file.

Here's the code that is activated at the push of a button (save button):

try{
    // Input the file location into Path variable 'p'
    //Cannot write to CSV files
    //Path p = Paths.get("C:\\Users\\myname\\Documents\\Stock Take Program\\tiger.csv");

    Path p = Paths.get("C:\\Users\\myname\\Desktop\\test.txt");

    //Read the whole file to a ArrayList
    List<String> fileContent = new ArrayList<>(Files.readAllLines(p));

    //Converting user input from editSerialField to a string
    String strSerial = editSerialField.getText();
    //Converting user input from editLocationField to a string
    String strLocation = editLocationField.getText();

    //This structure looks for a duplicate in the text file, if so, replaces it with the user input from editLocationField.
    for (int i = 0; i < fileContent.size(); i++)
    {
        if (fileContent.get(i).equals(strSerial))
        {
            fileContent.set(i, strLocation);
        }
        break;
    }

    // write the new String with the replaced line OVER the same file
    Files.write(p, fileContent);

    }catch(IOException e)
    {
        e.printStackTrace();
    }

My question is, how can I update my code to work with updating and replacing the contents of a .CSV file with the user input, the same way as it works for my .txt files.

When writing to a text file, it replaces only the first line, but when writing to a .CSV file, it does not replace anything.

Is there anyway I should be writing my code differently to replace text within a .CSV file.

Any help is greatly appreciated, thanks.

I'm an idiot. My '.CSV' file is actually titled tiger.csv as a text file. I've now saved an actual CSV version and it is now working.

Haha, thanks for the help guys.

Probably should be in another question, but the problem relating to the change only working on the first line is due to the break being called and shortcutting the loop on the first way round. Put it within the if block.

for (int i = 0; i < fileContent.size(); i++)
{
    if (fileContent.get(i).equals(strSerial))
    {
        fileContent.set(i, strLocation);
        break;
    }
}

Or leave it off completely if you want it to be able to update multiple lines.

HTH,

public static void main(String[] args) throws FileNotFoundException {

            String UserEntredValu="Karnataka";
            String csvFile = "C:/Users/GOOGLE/Desktop/sample/temp.csv";
            String line = "";
            String cvsSplitBy = ",";
            PrintWriter pw = null;
            pw = new PrintWriter(new File(csvFile));
            try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {

                while ((line = br.readLine()) != null) {

                    // use comma as separator
                    String[] country = line.split(cvsSplitBy);

                   for( int i = 0; i < country.length - 1; i++)
                   {
                       String element = country[i];

                       if(element.contains(UserEntredValu)){
                           String newEle=element.replace(element, "NEW INDIA");
                           pw.write(newEle);
                           System.out.println("done!");
                           pw.close();
                       }
                   }
             }

            } catch (IOException e) {
                e.printStackTrace();
            }

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM