Results 1 to 5 of 5
  1. #1
    sbutt is offline Member
    Join Date
    Apr 2009
    Posts
    18
    Blog Entries
    1
    Rep Power
    0

    Default Java file creation on Linux

    Hi All,
    It is actually a very simple problem but i can't get through with it.

    I am trying to create a simple txt or html file in my java program. It works fine but on my local windows machine (file gets created), but the file does not get created when i compile an drun it on linux machine. I have given all the project folders full rights 777, but still no file creation.

    Java Code:
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.PrintStream;
    
    
    public class FileTest {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    
    		FileOutputStream out;
    		PrintStream ps; // declare a print stream object
    		try {
    			// Create a new file output stream
    			out = new FileOutputStream("/opt/FileTest/bin/Test_1.html");
    
                            //OR
    
    			File file= new File("/opt/FileTest/bin/Test.txt");
    			if(!file.exists()){
    				file.createNewFile(); 
    			}
    			
    						
    			// Connect print stream to the output stream
    			ps = new PrintStream(out);
    
    			ps.println("HTTP");
    
    			System.err.println("Written successfully");
    
    			//Runtime.getRuntime().exec("explorer lcpAnalyzer.html");
    
    			ps.close();
    		} catch (Exception e) {
    			System.err.println("Error in writing to file");
    		}
    
    		
    	}
    
    }
    javac FileTest.java and java FileTest both work fine and i don't get any exception.
    There is no console message and also there are no Test_1.html and /Test.txt files created.

    Any idea?

    Thanks.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: Java file creation on Linux

    Print out the flag from the call to createNewFile (it returns a boolean).
    Just curious whether it thinks it's succeeded or not.

    Oh, and you don't have to do the if(fileExists) bit for that, createNewFile does that for you.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    sbutt is offline Member
    Join Date
    Apr 2009
    Posts
    18
    Blog Entries
    1
    Rep Power
    0

    Default Re: Java file creation on Linux

    It shows no output on my putty console after i do: java FileTest

    Java Code:
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.PrintStream;
    
    
    public class FileTest {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    
    		FileOutputStream out;
    		PrintStream ps; // declare a print stream object
    		try {
    			// Create a new file output stream
    			out = new FileOutputStream("/opt/integra/middleware/FileTest/src/Test_1.html");
    
    			File file= new File("/opt/integra/middleware/FileTest/src/Test.txt");
    			
    				System.out.println(file.createNewFile()); 
    			
    			
    						
    			// Connect print stream to the output stream
    			ps = new PrintStream(out);
    
    			ps.println("HTTP");
    
    			System.err.println("Written successfully");
    
    			//Runtime.getRuntime().exec("explorer lcpAnalyzer.html");
    
    			ps.close();
    		} catch (Exception e) {
    			System.err.println("Error in writing to file");
    		}
    
    		
    	}
    
    }

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: Java file creation on Linux

    Right, then I would strongly consider putting more println() statements in there to see where the code is going.
    Also do e.printStackTrace() in the catch block.
    You should be seeing something if that code is running.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    sabre150 is offline Member
    Join Date
    Jul 2012
    Location
    Earth
    Posts
    75
    Rep Power
    0

    Default Re: Java file creation on Linux

    This thread has been posted in multiple forums.

Similar Threads

  1. Replies: 0
    Last Post: 05-18-2012, 01:29 PM
  2. Jar File Creation
    By snickers10m in forum New To Java
    Replies: 1
    Last Post: 03-08-2012, 11:02 PM
  3. Replies: 4
    Last Post: 07-27-2011, 11:57 AM
  4. Replies: 0
    Last Post: 11-26-2010, 10:10 PM
  5. Archiving File on Shared Folder in Linux using java code
    By ajmerasunny in forum Advanced Java
    Replies: 1
    Last Post: 10-30-2009, 07:15 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
  •