Results 1 to 5 of 5
Thread: IO stream
- 04-10-2011, 08:03 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 12
- Rep Power
- 0
IO stream
hi ^_^
I have code to check if the file or directory can be read i'll display them content, and to check if they can be write so write to file extra sentence but for directory , create a new directory with the name "newDirectory" and create a new file called "newFile" inside that new directory "newDirectory". Otherwise, writing is not permitted and you display "Sorry we can't" on screen. then Rename the file newFile and give it a new name " oldFile"....
so I try to write simple code, it work with file but not with directory I don't know why??
can anyone help me to make the code work well ^_^
Java Code:import java.util.*; import java.io.*; import java.util.Date; public class Q1 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner scan=new Scanner(System.in); System.out.println("enter file or folder pathname :"); String s=scan.next(); try{ File file=new File(s); if(file.exists()!=true) System.out.println("File or directory does not exist-Bye"); else if(file.isFile()&&file.canRead()){ BufferedReader input=new BufferedReader(new FileReader(s)); String ch; while((ch=input.readLine()) != null) System.out.println(ch);} else if(file.isDirectory()&&file.canRead()){String [] sl=file.list(); for (int i=0; i<sl.length;i++) System.out.println(sl[i]);} if(file.isFile()&&file.canWrite()){ BufferedWriter bw=new BufferedWriter(new FileWriter(s,true)); bw.newLine(); bw.write("Last but not least"); bw.close(); } else if(file.isDirectory()&& file.canWrite()){ File fd=new File(s); fd.mkdir(); }else System.out.println("sorry"); File file2=new File("newFile.txt"); file2.renameTo(new File("oldFile.txt")); }//end try catch(FileNotFoundException ex){ System.err.println(ex.getMessage()); }//end catch catch(IOException ex){ System.err.println(ex.getMessage()); }//end catch }//end main }//end class
- 04-10-2011, 12:13 PM #2
i can't explain to youcan anyone help me to make the code work well ^_^
but you need to check the main class\
please help her
good luckI did not understand anything in Java:(
Therefore, future lost:eek:
-
It works except for the mkdir part because you have to change s for the directory to be a new one. e.g.,
Java Code:else if (file.isDirectory() && file.canWrite()) { File fd = new File(s + "/foo"); // **** note change here **** fd.mkdir(); } else
-
- 04-10-2011, 08:08 PM #5
Member
- Join Date
- Apr 2011
- Posts
- 12
- Rep Power
- 0
Similar Threads
-
How do I know that byte of stream is finished?
By mani_minhaj in forum New To JavaReplies: 2Last Post: 02-26-2010, 12:08 PM -
TCP stream cypher
By Koren3 in forum NetworkingReplies: 2Last Post: 05-20-2009, 10:08 PM -
stream redirection
By Ed in forum New To JavaReplies: 1Last Post: 07-02-2007, 04:34 PM -
video stream
By Alan in forum Advanced JavaReplies: 2Last Post: 05-17-2007, 08:12 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks