Not creating file (Java)

58 views Asked by At

For some reason I keep on getting the following error when I compile my code. I have the correct preprocessor directives (import statements), and no syntax errors but whenever I compile my code I am given

2

There are 2 answers

0
KDP On

probably,your dateFile is empty which might be the reason for this exception

public int nextInt()

Scans the next token of the input as an int. An invocation of this method of the form nextInt()

Returns: the int scanned from the input

Throws: InputMismatchException - if the next token does not match the Integer regular expression, or is out of range

NoSuchElementException - if input is exhausted

IllegalStateException - if this scanner is closed

refer http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#Scanner(java.io.File)

0
Simone D. On
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Scanner;
import javax.swing.JOptionPane;

public class Reader
{
  public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException, IOException
  {

    File dateFile = new File("test.txt");
    FileWriter fw = new FileWriter(dateFile);
    BufferedWriter bw = new BufferedWriter(fw);
    Scanner reader = new Scanner(dateFile);

    try
    {
      // if file doesnt exists, then create it
      if (!dateFile.exists())
      {
        dateFile.createNewFile();
        bw.write("1");
        bw.close();
        System.out.println("Done");
      }else
      {
        int duration;
        String ans = JOptionPane.showInputDialog ("Enter the amount of problems per training session (with number in minutes):");

        while(!ans.matches("[0-9]+"))
        {
          ans = JOptionPane.showInputDialog ("Please re-enter the amount of problems per training session (with number in minutes):" );
        }

        duration = Integer.parseInt(ans);
        System.out.println(duration);
        bw.write(""+duration);
        bw.flush();

        int numSessions = reader.nextInt();
        System.out.println("Number of sessions is: " + numSessions);
        String fileName = ("sessionNumber"+numSessions);
        File newSession = new File(""+fileName+".txt");
        System.out.println(fileName);

        if (!newSession.exists())
        {
          newSession.createNewFile();
          System.out.println("IT DOES NOT EXIST!");
        }

        fw = new FileWriter(newSession.getAbsoluteFile());
        bw = new BufferedWriter(fw);

        bw.write(duration);
        bw.close();
      }
    } catch (IOException e)
    {
      e.printStackTrace();
    }
  }
}

Now it should work. I add this two lines:

bw.write(""+duration);
bw.flush();

you were never write in the File. Remember to .flush() your buffer.. if you want it to write on the file before you close it! notice that i changed the path of the file to let it work on my pc so change it again