Results 1 to 11 of 11
Thread: Creating Files in Java?
- 03-02-2011, 12:05 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 63
- Rep Power
- 0
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?
this will only create a file object, but i want it to create a file .txt file in a folder i specified.Java Code:File file = new File(fullName);
Also how do i delete a text file through java code?Last edited by Bagzli; 03-02-2011 at 12:11 AM.
- 03-02-2011, 12:06 AM #2
Write to it.
-
- 03-02-2011, 12:21 AM #4
Member
- Join Date
- Feb 2011
- Posts
- 63
- Rep Power
- 0
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);this is the error i get when i try to run that code, if i create the text file manually it works perfectly.Java Code:Exception in thread "main" java.io.FileNotFoundException: userlogs\97902log.txt (The system cannot find the file specified)
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.
- 03-02-2011, 12:27 AM #5
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.
- 03-02-2011, 12:54 AM #6
Member
- Join Date
- Feb 2011
- Posts
- 63
- Rep Power
- 0
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?
- 03-02-2011, 01:10 AM #7
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.
- 03-02-2011, 01:33 AM #8
Member
- Join Date
- Feb 2011
- Posts
- 63
- Rep Power
- 0
When it doesnt work
when it worksJava 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)
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.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
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.
- 03-02-2011, 01:41 AM #9
- 03-02-2011, 01:46 AM #10
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.
- 03-02-2011, 01:56 AM #11
Member
- Join Date
- Feb 2011
- Posts
- 63
- Rep Power
- 0
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
-
Creating and running Java .ini files
By Camzie in forum NetBeansReplies: 4Last Post: 11-25-2009, 12:42 PM -
Creating files stopped creating...
By Dieter in forum Advanced JavaReplies: 3Last Post: 09-25-2009, 11:45 PM -
Creating preview images of files in java
By sandeepk84@gmail.com in forum Advanced JavaReplies: 3Last Post: 07-04-2009, 10:29 PM -
Creating .Jar files
By Deathmonger in forum New To JavaReplies: 6Last Post: 06-22-2008, 11:59 PM -
Creating ZIP files
By Java Tip in forum Java TipReplies: 0Last Post: 03-03-2008, 05:14 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks