0

What I am doing is creating two nodes that will talk to each other through text files for example: Node 0 is neighbors with Node 1 and vice versa. Node 0 will open up text file from0to1.txt and Node 1 will open up a textfile from1to0.txt.

I verify the creation of these files in this code:

for(Integer i: neighbors){
        File file = new File("from" + myId + "to" + i + ".txt");
        try{
            boolean fileMade = file.createNewFile();
            if(!fileMade){
                System.err.println("Node " + myId + ": File could not be created.\n Please delete the files before trying again."); 
                System.exit(1);
            }
            /**
            boolean fileMade = file.createNewFile();
            while(!fileMade){
                System.out.println("Node " + myId + ": File is already present.");
                System.out.println("Node " + myId + ": Deleting file...");
                file.delete();
                System.out.println("Node " + myId + ": File deleted");
                System.out.println("Node " + myId + ": Trying again...");
                fileMade = file.createNewFile();
            }
            */
            System.out.println("Node " + myId + ": File " + file.getName() + " successfully created.");
        }
        catch(Exception e){
            e.printStackTrace();
        }

Once these files have been opened up, I am opening them to write into them. Each node will be sensing data from these "channels" that are text files. When reading, I am opening up these text files with a RandomAccessFile. When I am writing, I am opening up these text files with a FileWriter/BufferedWriter.

The problem is that when I attempt to open up the text files for reading with a RandomAccessFile, it throws a FileNotFoundException. I have attempted to run f.exist() and it also turns up as false. Why is it not creating the file/not acknowledging that the file exists?

Here is the code:

for(Integer n: neighbors){
        RandomAccessFile raf = null;
        File f = new File("from" + n + "to" + nodeID + ".txt");
        System.out.println(f.exists());
        System.out.println(f.canRead());
        //FileReader fr = null;
        try{
            System.out.println("Trying to open file: " + "from" + n + "to" + nodeID + ".txt");
            //System.out.println("Node " + nodeID + ": Setting up Random Access File");
            //fr = new FileReader(new File("from" + n + "to" + nodeID + ".txt"));
            //System.out.println(fr.read());
            //fr.close();
            raf = new RandomAccessFile(f, "r");
            raf.seek(offsetList.get(n));
            /**
  • 2
    Right. It looks like you forgot to ask a question. What is the problem with the above code? What did you expect to happen, what happened instead? If exceptions are involved, at what line did they occur? – Erwin Bolwidt May 03 '18 at 02:43
  • @ErwinBolwidt My apologies, I have edited the post. – Thomas Yang May 03 '18 at 02:58
  • Are these two completely separate programs that you're running at the same time? If so, maybe you have a race condition, where the "reader" program tries to open the file before the "writer" program has a chance to create the file. – cmosher01 May 03 '18 at 03:47

0 Answers0