Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: problem with File Stream

  1. #1
    Member
    Join Date
    Nov 2010
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default problem with File Stream

    Hello,
    Please tell me what is the problem with my code:in the following code i try to create a directory.

    import java.io.*;
     
    public class MkDir
    {
    	public static void main (String[] args) 
    	{
    		File f1;
    		f1.mkdir("c:/rr");
    	}
    }


  2. #2
    Member
    Join Date
    Nov 2010
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: problem with File Stream

    also, I have a proble with the following code
    first file:

     
    import java.io.*;
     
    public class OnlyExt implements FilenameFilter
    {
    	String ext;
     
    	public void OnlyExt(String ext)
    	{
    		this.ext="." + ext;
    	}
     
    	public boolean accept(File dir, String name)
    	{
    		return name.endsWith(ext);
    	}
    }


    second file:

     
    import java.io.*;
     
    public class HTMLOnly
    {
    	public static void main (String[] args)
    	{
    		String dirname="/java";
    		File f1= new File(dirname);
    		FilenameFilter only = new OnlyExt("html");
    		String s[] = f1.list(only);
     
    		for (int i=0; i < s.length; i++) 
                   System.out.println(s[i]);
    	}
     
    }

  3. #3
    Member
    Join Date
    Nov 2010
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: problem with File Stream

    in the following code i try to display the contents of a txt file using FileInputStream, and the output is
    ?
    ?
    892745528
    14
    which is absolutely different from the contents of the txt file.

    import java.io.*;
    public class InputStream1
    {
    	public static void main (String[] args)
    	{
    		String path="d:/t.txt";// the file contains 123456789
    		                       //abcd
    		                       //08
    		                       //9
    		try
    		{
     
    		   FileInputStream fis=new FileInputStream(path);
    		   DataInputStream dis=new DataInputStream(fis);
    		   System.out.println (dis.readChar());
    		   System.out.println (dis.readChar());
    		   System.out.println (dis.readInt());
    		   System.out.println (dis.available());
    		   dis.close();
    		}
    		catch(FileNotFoundException fe)
    		{
    			System.out.println("FileNotFoundException : " + fe);
    		}
    		catch(IOException ioe)
    		{
    			System.out.println("IOException : " + ioe);
    		}
    	}
    }
    Last edited by amr; April 11th, 2011 at 02:22 PM.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: problem with File Stream

    it looks like you're trying to read text. The best way to read text in is to not use the DataInputStream, but rather a BufferedReader or a Scanner object. These read text data rather than the raw bytes of the file.

  5. #5
    Member
    Join Date
    Nov 2010
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: problem with File Stream

    thanx
    but can u tell me
    1-why we must use try and catch when dealing with files??
    2-how to create a directory "see the first post"

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: problem with File Stream

    When dealing with files, Java could possibly throw an IOException when the file couldn't be read/written to. There could be many reasons for this. IOException is a checked exception, i.e. you must always catch it (or alternatively you can propagate the exception up, though generally you shouldn't propagate exceptions out of your application).

    It looks like you're trying to create a directory in a write-protected region (I'm assuming you're using Windows Vista or 7). The only solution is to launch your program with administrative rights, or to change where you're trying to create the directory in. Launching Java applications with administrative rights is particularly tricky, as you need to start the JVM with administrative rights.

    If you're running your program from the command-line, you can start the command prompt with administrative rights, or set the JVM to always start with administrative rights (note: this is not recommended).

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,273
    Thanks
    177
    Thanked 822 Times in 765 Posts
    Blog Entries
    5

    Default Re: problem with File Stream

    To add to helloworld's advice:

    File f1;
    f1.mkdir("c:/rr");

    f1 was never instantiated == NullPointerException (did you see this exception when running this SSCCE?) First instantiate the file object, then call mkdir (and see the API for file...mkdir returns a boolean value as to whether the folder was created or not)
    File myDirectory = new File("my pile path");
    if (!myDirectory.mkdir()){
    //I could not create the directory
    }

Similar Threads

  1. Read stream of data
    By mapred in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: October 26th, 2012, 12:38 PM
  2. Replies: 1
    Last Post: April 7th, 2011, 01:32 PM
  3. Object and text file stream code problmes
    By Kakashi in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 3rd, 2011, 03:34 PM
  4. UTF - 8 / Byte Stream
    By JavaCODER in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: October 16th, 2010, 02:26 PM
  5. Stream Tokenizer
    By x3rubiachica3x in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 12th, 2010, 01:05 PM