0
public static void update(String fileName, String idType, String id, String updatedData[] ) throws Exception{

    char fileOutput[];
    String wholeData;
    String tkn, idtp, idf;      

    File myFileU  = null;
    File tempFile = null;
    FileReader finU = null;
    FileWriter fwU = null;
    Scanner frU = null;



    try{

        finU = new FileReader(myFileU = new File("\\" +fileName + ".txt"));
        fileOutput = new char[(int) myFileU.length()];
        finU.read(fileOutput);
        finU.close();

        //System.out.println(myFileU.getCanonicalPath());

        tempFile = new File("temp.txt");
        tempFile.createNewFile();
        fwU = new FileWriter(myFileU, false);


         wholeData = new String(fileOutput);

        frU = new Scanner(wholeData);

        frU.useDelimiter(";");

        while(frU.hasNext()){   
            idtp = frU.next();
            idf = frU.next();
            if(idtp.equals(idType) && idf.equals(id)){
                fwU.write( ";" + idType + ";" + id);
                for(int i=0; i< updatedData.length; i++){
                    fwU.write(";" + updatedData[i]);

                }
                fwU.write(";" + System.lineSeparator());

                frU.nextLine();
            }
            if(!idf.equals(id))
            fwU.write(";" + idtp + ";" + idf);
            tkn = frU.nextLine();
            fwU.write(tkn);
            fwU.write(System.lineSeparator());
            if(idf.equals(autoSerial(fileName, idType)))
                break;
        }

        fwU.flush();
        fwU.close();


        }
    catch(IOException e){
        System.out.println("error in opening the file U " + e);     
    }
    finally{

    }
}

The above method is meant to overwrite the file it is reading from. What it is supposed to do is read from the file, replace the record specified by the user with updated data and overwrite the file with updated data, but it doesn't overwrite the file instead appends the updated record at the end of the file and gives (though if I save the data to a separate file it saves the updated data correctly to it):

java.io.FileNotFoundException: \Schedules.txt (The system cannot find the file specified)

While the file is there and it has read data from it too? Any clue? I'm new to Java!

Biffen
  • 6,249
  • 6
  • 28
  • 36
  • 1
    Where is your file located? the full location of your file? It gives exception because it cannot find it. If the file is located in the same directory then remove the \\ and try again like **File(fileName + ".txt")** – Raf Dec 08 '15 at 10:55
  • There is an operating system which uses backslashes and which is not Windows? Interesting. – Tom Dec 08 '15 at 11:02

1 Answers1

1

Your issue is clearly with opening the file using Java. You seem to be getting confused with file path. Following are examples of how you open a file using different locations, etc.

Let's assume your file is named abc.txt and is located in C:\ drive under test_stackoverflow directory then you your path would be as shown below:

FileReader reader = new FileReader(new File("C:\\test_stackoverflow\\abc.txt")); 

Notice the double slashes, that is how you skip a slash.

If your file is in the same directory as your java class then the path is as shown below without any slashes

FileReader reader = new FileReader(new File("test.txt")); 

Let's assume that the file that you wish to read is one folder above (src) where your java class is then

FileReader reader = new FileReader(new File("src\\test.txt"));

If you are using OSX then you can do something in the following lines

FileReader reader = new FileReader(new File("/Users/Raf/Desktop/abc.txt"));
Raf
  • 7,505
  • 1
  • 42
  • 59