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.
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:
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.
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.
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"
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).
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 }