11 Replies - 513 Views - Last Post: 21 June 2009 - 07:12 PM Rate Topic: -----

#1 Solris  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 09-November 08

creating a file

Post icon  Posted 21 June 2009 - 04:01 PM

Im new to java. I just learned how to create a file, the type u can store data in( File data = new File). I have a question how can i go about about creating a file and have it install or appear certain directories, like on my desktop?

I tried using mkdir, but im not really sure how to write the code. I looked up several sites that show how to use mdkir, but they didn't seem to help me

Hope someone can help.
Is This A Good Question/Topic? 0
  • +

Replies To: creating a file

#2 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: creating a file

Posted 21 June 2009 - 04:39 PM

So you just want to make a directory? Just create a file of the directory and call the mkdir method on it
File f = new File("C:/Users/Nick/Desktop");
boolean b = f.mkdir();

Was This Post Helpful? 0
  • +
  • -

#3 Solris  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 09-November 08

Re: creating a file

Posted 21 June 2009 - 05:47 PM

Hey nick, thanks for replying to my problem.

From the example you showed me, i created this small code, to get a better grasp and understanding how mkdir works. But i ran into a small promblem, the code executes without any errors, but the file never apperar on my desktop, do uou know why?

Here the code

import java.io.*;

public class CreateFile1{
	public static void main(String[] args) throws IOException{
	  File f = new File("C:/Users/Solris/Desktop");
	  boolean  b = f.mkdir();
BufferedWriter output = new BufferedWriter(new FileWriter(f));
output.write("Testing");
output.close();
}
}


Was This Post Helpful? 0
  • +
  • -

#4 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: creating a file

Posted 21 June 2009 - 05:59 PM

Make sure you do your c: location correctly. You didnt just replace your name with mine did you? Also, I dont know what your root is set to so the location might look a bir different to that,
Was This Post Helpful? 0
  • +
  • -

#5 Solris  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 09-November 08

Re: creating a file

Posted 21 June 2009 - 06:12 PM

I checked the directory again, it seems to be right. Just to make sure the directory is correct, i created a new folder on my desktop and used that directoy.

Please bare with me, like i said im new to java, but Im a little confused when you talked about root of my location?
Was This Post Helpful? 0
  • +
  • -

#6 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: creating a file

Posted 21 June 2009 - 06:16 PM

the mkdir method creates the directory for you. You shouldnt be making it yourself. What ide are you using? netbeans?
Was This Post Helpful? 0
  • +
  • -

#7 Solris  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 09-November 08

Re: creating a file

Posted 21 June 2009 - 06:20 PM

At the moment im using netbeans IDE 6.5.1
Was This Post Helpful? 0
  • +
  • -

#8 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: creating a file

Posted 21 June 2009 - 06:37 PM

ok. For the time being then, change this line to
File f = new File("test");

And see if it makes a directory in your netbeans project folder. I made a mistake in my original post anyway, didnt specify a name for the directory.
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: creating a file

Posted 21 June 2009 - 06:39 PM

View PostSolris, on 21 Jun, 2009 - 05:20 PM, said:

At the moment im using netbeans IDE 6.5.1

has nothing to do
Java is Java it is not related to the IDE you are using
You might using Notepad and the command prompt
>javac MyClass.java
you would have the same problem
Was This Post Helpful? 0
  • +
  • -

#10 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: creating a file

Posted 21 June 2009 - 06:45 PM

Sorry, my fault, I asked what ide he was using so i could instruct him to create it straight into the root directory rather than mess about with adding all the c: etc. I am hoping anyways, that just giving it a name like he is going to do, it will place the new directory straight into his project folder.
Was This Post Helpful? 0
  • +
  • -

#11 Solris  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 09-November 08

Re: creating a file

Posted 21 June 2009 - 07:06 PM

Hey nick, thx for all your help, but i made the changes as you directed, as you can see. But the file never appears on my desktop or in the project directory.

import java.io.*;

public class CreateFile1{
	public static void main(String[] args) throws IOException{
	  File f = new File("test");
	  boolean  b = f.mkdir();
BufferedWriter output = new BufferedWriter(new FileWriter(f));
output.write("Testing");
output.close();
}
}

Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: creating a file

Posted 21 June 2009 - 07:12 PM

View Postnick2price, on 21 Jun, 2009 - 05:45 PM, said:

Sorry, my fault, I asked what ide he was using so i could instruct him to create it straight into the root directory rather than mess about with adding all the c: etc. I am hoping anyways, that just giving it a name like he is going to do, it will place the new directory straight into his project folder.

No offense Nick
just want to make sure we are not confusing IDE and Java problems
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1