Results 1 to 11 of 11
  1. #1
    Bagzli is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default Creating Files in Java?

    How do i get Java to create a new text file for me? and what happens when i try to create a file that already exists?

    Java Code:
    File file = new File(fullName);
    this will only create a file object, but i want it to create a file .txt file in a folder i specified.

    Also how do i delete a text file through java code?
    Last edited by Bagzli; 03-02-2011 at 12:11 AM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Write to it.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Bagzli View Post
    How do i get Java to create a new text file for me? and what happens when i try to create a file that already exists?

    Java Code:
    File file = new File(fullName);
    this will only create a file object, but i want it to create a file .txt file in a folder i specified.

    The fullName part above can be a fully qualified file name including folder extension and all. What happens when you try this on a file that already exists?

  4. #4
    Bagzli is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default

    Object 1
    Java Code:
    Student mrX = new Student(userName, password);
    
    public Student(String studentId, String password) throws FileNotFoundException
    	{
    		this.studentId = studentId;
    		this.password = password;
    		//name of log file
    		
    		String userLogId = "userlogs/" + studentId + "log.txt";
    		File userLog = new File(userLogId);
    Java Code:
    Exception in thread "main" java.io.FileNotFoundException: userlogs\97902log.txt (The system cannot find the file specified)
    this is the error i get when i try to run that code, if i create the text file manually it works perfectly.

    Just to verify what i was saying, i want java to physically create a text file for me and i also want to know how to delete it.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Exception in thread "main" java.io.FileNotFoundException: userlogs\97902log.txt (The system cannot find the file specified)

    I could not read the error.

    You are trying to create/find a file in the userlogs directory. Where is this directory? The JVM is trying to find it in your current working directory which evidently is not correct.

  6. #6
    Bagzli is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default

    ok you guys are not understanding me probably since i'm doing a poor job explaining. How about this, can you give me lines of code that will create a file in a C:/program files/Java directory/hello.txt

    I want java to create hello.txt for me. Also how do i delete it as well with java code?

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Once again we understand fine. It is you who does not understand. I explained fully why you get the FileNotfoundException because the userlogs directory DOES NOT exist where you said it does. So either use a full path to the userlogs directory or move it into your current working directory.

  8. #8
    Bagzli is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default

    When it doesnt work
    Java Code:
    Enter your username:
    97902
    Enter your password:
    hsawrty
    Congratulations you have logged in
    Please select one of the following options:
    1. View Change Log from last time modified
    1
    Exception in thread "main" java.io.FileNotFoundException: userlogs\97902log.txt (The system cannot find the file specified)
    	at java.io.FileInputStream.open(Native Method)
    	at java.io.FileInputStream.<init>(Unknown Source)
    	at java.util.Scanner.<init>(Unknown Source)
    	at courseChanger.FileReader.lineCount(FileReader.java:44)
    	at courseChanger.MainFlow.main(MainFlow.java:82)
    when it works
    Java Code:
    Enter your username:
    99707
    Enter your password:
    yertgfs
    Congratulations you have logged in
    Please select one of the following options:
    1. View Change Log from last time modified
    1
    test1
    test2
    test3
    the user name is actually part of the file name. the directory userlogs exists as you can see but the actual file in the first example does not exist and i want to create it. In the second example the actual file exists and it reads from it.

    I am sorry to frustrate you but the directory exists. The program crashes when it tries to open a file that doesn't exist (no wonder). The problem here is that the file is not being created and i don't know why. Here is the code i use to create it.

    Java Code:
    String userLogId = "userlogs/" + studentId + "log.txt";
    		File userLog = new File(userLogId);
    Last edited by Bagzli; 03-02-2011 at 01:38 AM.

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by Bagzli View Post
    the directory userlogs exists as you can see
    No I can't see.

    Post your code as well as your directory listing.

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by Bagzli View Post
    The program crashes when it tries to open a file that doesn't exist (no wonder).
    What do you mean by "open a file"?

    When you create a File object for a file that does not exist it does not automagically create it for you. If you then attempt to read from that file then yes it will crash. As I stated in my first reply a file will only be created for your File object when something is written to that file.

    If you do:

    Create File object
    Read from File object

    it will crash as the actual file does not exist.

    If you do:

    Create File object
    Write to File
    Read from File object

    then it will work as the actual file has now been created.

  11. #11
    Bagzli is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default

    thats what the problem was:

    Your words:

    "If you do:

    Create File object
    Write to File
    Read from File object

    then it will work as the actual file has now been created"

    Now I have fixed it, thanks so much. I was creating the file without writing text in it so it would not create properly and then it would try to read it except it doesn't exist.
    Last edited by Bagzli; 03-02-2011 at 01:58 AM.

Similar Threads

  1. Creating and running Java .ini files
    By Camzie in forum NetBeans
    Replies: 4
    Last Post: 11-25-2009, 12:42 PM
  2. Creating files stopped creating...
    By Dieter in forum Advanced Java
    Replies: 3
    Last Post: 09-25-2009, 11:45 PM
  3. Creating preview images of files in java
    By sandeepk84@gmail.com in forum Advanced Java
    Replies: 3
    Last Post: 07-04-2009, 10:29 PM
  4. Creating .Jar files
    By Deathmonger in forum New To Java
    Replies: 6
    Last Post: 06-22-2008, 11:59 PM
  5. Creating ZIP files
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-03-2008, 05:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •