Results 1 to 8 of 8
Thread: How to create a file
- 05-25-2007, 07:38 PM #1
Member
- Join Date
- May 2007
- Posts
- 4
- Rep Power
- 0
- 05-25-2007, 08:31 PM #2
Member
- Join Date
- May 2007
- Posts
- 7
- Rep Power
- 0
Hello Alpha,
You can use the following code to create a file:
And for reading from or writing to the file, you can use following tips:Java Code:try { File file = new File("filename"); // Create file if it does not exist boolean success = file.createNewFile(); if (success) { // File did not exist and was created } else { // File already exists } } catch (IOException e) { }
Java Tips - How to Read File in Java
Java Tips - How to write file in Java
- 05-25-2007, 08:44 PM #3
Member
- Join Date
- May 2007
- Posts
- 4
- Rep Power
- 0
Thanks Faruk, this is exactly what i want.
- 01-04-2012, 09:59 PM #4
Member
- Join Date
- Jan 2012
- Posts
- 3
- Rep Power
- 0
Re: How to create a file
i am having the same problem but im trying to make it in a specifick directory(C:\Users\tv\AppData\Roaming\MS2-torsteinv\MS2-dat\CD.txt) and it promts out that the directory dosent exists! cant it just make it?how can i solve this problem?
- 01-06-2012, 06:20 AM #5
Member
- Join Date
- Feb 2010
- Posts
- 80
- Rep Power
- 0
Re: How to create a file
as it was saying, the directory doesn't exist. what kind of file would you even be creating on that directory?
[why are you annoyed with my sig?]
- 01-06-2012, 03:35 PM #6
Member
- Join Date
- Jan 2012
- Posts
- 3
- Rep Power
- 0
Re: How to create a file
i am making a installer to my game but do i have to create the directory first maybe?
- 01-06-2012, 04:37 PM #7
Re: How to create a file
Could be. The mkdirs() method on the File class will do this for you! Take a look at it on the File page of the java api.i am making a installer to my game but do i have to create the directory first maybe?
- 01-06-2012, 07:10 PM #8
Member
- Join Date
- Jan 2012
- Posts
- 3
- Rep Power
- 0
Re: How to create a file
ok i have used your mkdirs soulution but it semms as if i got no access. here is my code if its useful to fixing my problem:
Downloader.java
Launcher.javaJava Code:import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.MalformedURLException; import java.net.URL; import java.util.Scanner; public class Downloader { public static void DownloadFile(String Indir,String Outdir,String Inname,String Outname) throws MalformedURLException, IOException{ InputStream Input = new URL(Indir+"/"+Inname).openStream(); if(new File(Outdir+"/"+Outname).exists()){ new File(Outdir+"/"+Outname).delete(); } File f = new File(Outdir); f.mkdirs(); OutputStream Output = new FileOutputStream(Outdir+"/"+Outname); Scanner scan = new Scanner(Input); while(scan.hasNext()){ byte[] bytes = scan.nextLine().getBytes(); Output.write(bytes, 0, bytes.length); } Input.close(); Output.close(); } }
Java Code:import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.util.Scanner; import javax.swing.JOptionPane; public class Launcher{ private static final String dir = "http://tv-w7:8000/"; public static void main(String[] args){ new Launcher(args); } public Launcher(String[] args){ if(args.length==1){if(args[0].equals("f")){ try { JOptionPane.showMessageDialog(null, "game is getting cleaned, press OK to proseed"); DownloadNewVersion(false,false); } catch (IOException e1) { e1.printStackTrace(); } } }else{ try { Download(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } public void Download() throws MalformedURLException, IOException{ if(new File(System.getenv("APPDATA")+"/MS2-torsteinv/MS2-bin/no/torsteinv/MarsSettlement2/Client/Client.class").exists()){ InputStream AVIS = new URL(dir+"MS2-dat/CD.txt").openStream(); InputStream CVIS = new FileInputStream(System.getenv("APPDATA")+"/MS2-torsteinv"); Scanner CVS = new Scanner(CVIS); Scanner AVS = new Scanner(CVIS); String CV = CVS.nextLine(); String AV = AVS.nextLine(); CVIS.close(); AVIS.close(); if(!CV.equals(AV))DownloadNewVersion(true,true); }else{ DownloadNewVersion(true,false); } } private void DownloadNewVersion(boolean promt,boolean N) throws IOException { if(promt && N)if(JOptionPane.showConfirmDialog(null,"A new verson is avalible. Download?")==1); Runtime.getRuntime().exec("java -classpath "+System.getenv("APPDATA")+"/MS2-torsteinv"); Scanner scan = new Scanner(new URL(dir+"MS2-register.txt").openStream()); String CL = ""; while(scan.hasNext()){ CL = scan.nextLine(); Downloader.DownloadFile(dir+CL.split(":")[0],System.getenv("APPDATA")+"/MS2-torsteinv/"+CL.split(":")[0],CL.split(":")[1],CL.split(":")[1]); } start(); } private void start(){ try { Runtime.getRuntime().exec("java "+System.getenv("APPDATA")+"/MS2-torsteinv/MS2-bin/no/torsteinv/MarsSettlement2/Client/Client.class"); } catch (IOException e) { e.printStackTrace(); } System.exit(0); } }
Similar Threads
-
Vector create
By Warren in forum New To JavaReplies: 4Last Post: 03-02-2010, 02:42 AM -
can java.io.File create a list of all files and folders.
By MattStone in forum New To JavaReplies: 20Last Post: 12-17-2007, 03:20 PM -
Create a new variable
By mathias in forum New To JavaReplies: 1Last Post: 08-07-2007, 06:48 AM -
How to create a xml file from oracle sql query
By boy22 in forum XMLReplies: 1Last Post: 07-24-2007, 12:15 AM -
Create XML From XSD
By Jack in forum XMLReplies: 1Last Post: 07-09-2007, 12:56 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks