Results 1 to 8 of 8
  1. #1
    Alpha is offline Member
    Join Date
    May 2007
    Posts
    4
    Rep Power
    0

    Default How to create a file

    I need to create a file on disk and write to it. How can i do this with Java?

  2. #2
    FaRuK is offline Member
    Join Date
    May 2007
    Posts
    7
    Rep Power
    0

    Default

    Hello Alpha,

    You can use the following code to create a file:

    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) {
        }
    And for reading from or writing to the file, you can use following tips:

    Java Tips - How to Read File in Java
    Java Tips - How to write file in Java

  3. #3
    Alpha is offline Member
    Join Date
    May 2007
    Posts
    4
    Rep Power
    0

    Default

    Thanks Faruk, this is exactly what i want.

  4. #4
    Torstein99 is offline Member
    Join Date
    Jan 2012
    Posts
    3
    Rep Power
    0

    Default 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?

  5. #5
    clydedoris is offline Member
    Join Date
    Feb 2010
    Posts
    80
    Rep Power
    0

    Default 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?]

  6. #6
    Torstein99 is offline Member
    Join Date
    Jan 2012
    Posts
    3
    Rep Power
    0

    Default Re: How to create a file

    i am making a installer to my game but do i have to create the directory first maybe?

  7. #7
    quad64bit's Avatar
    quad64bit is online now Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,324
    Rep Power
    7

    Default Re: How to create a file

    i am making a installer to my game but do i have to create the directory first maybe?
    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.

  8. #8
    Torstein99 is offline Member
    Join Date
    Jan 2012
    Posts
    3
    Rep Power
    0

    Default 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
    Java 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();
    	}
    }
    Launcher.java
    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

  1. Vector create
    By Warren in forum New To Java
    Replies: 4
    Last Post: 03-02-2010, 03:42 AM
  2. Replies: 20
    Last Post: 12-17-2007, 04:20 PM
  3. Create a new variable
    By mathias in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 07:48 AM
  4. Replies: 1
    Last Post: 07-24-2007, 01:15 AM
  5. Create XML From XSD
    By Jack in forum XML
    Replies: 1
    Last Post: 07-09-2007, 01:56 AM

Posting Permissions

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