1 PackageShi;2 3 ImportJava.io.*;4 5 Public classWenjianio {6 Public Static voidMain (String agrs[]) {7FileInputStream fis=NULL;8File f=NewFile ("F:/test/javakc.txt");9 Ten Try { Onefis=NewFileInputStream (f); A byte[]arr=New byte[(int) F.length ()]; - Fis.read (arr); -String str=NewString (arr); the System.out.println (str); - -}Catch(FileNotFoundException e) { - //TODO auto-generated Catch block + e.printstacktrace (); -}Catch(IOException e) { + //TODO auto-generated Catch block A e.printstacktrace (); at } - finally{ - if(fis!=NULL){ - Try{ - fis.close (); - } in Catch(IOException e) { - e.printstacktrace (); to } + } - } the * } $ Panax Notoginseng}View Code
Java reads and writes content to a file
PackageShi;ImportJava.io.*; Public classWenjianio { Public Static voidMain (String agrs[]) {FileInputStream fis=NULL; FileOutputStream Fos=NULL; File F=NewFile ("F:/test/javakc.txt"); Try {//write content to a filefos=NewFileOutputStream (f); String s= "Your little ez, have gone crooked"; byte[]b=s.getbytes (); Fos.write (b); //read content to a filefis=NewFileInputStream (f); byte[]b2=New byte[(int) F.length ()]; Fis.read (B2); String Str=NewString (B2); System.out.println (str); } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } finally{ if(fis!=NULL){ Try{fos.close (); Fis.close (); } Catch(IOException e) {e.printstacktrace (); } } } }}
Reading the contents of a file in Java