The abstract base class of byte stream: InputStream, the abstract base class of OutputStream
character stream: Reader, Writer
The subclass names derived from these four classes are all suffixed with the parent class name as the subclass name. Such as: InputStream subclass FileInputStream, Reader subclass FileReader
################################################## #file
write, byte output stream
private static final String LINE_SEPARATOR = System.getProperty("line.separator"); // Get the system newline for newline public static void main(String[] args) throws IOException { System.out.println(LINE_SEPARATOR); String s = "花"+LINE_SEPARATOR+"awegadjgaw"; File dir = new File("abc"); if(!dir.exists()){ dir.mkdir (); } FileOutputStream fos = new FileOutputStream("abc\\ai.txt", true ); // true means can continue to write fos.write(s.getBytes()); // write information System.out.println("ok" ); fos.close(); }
##########Exception Handling
public static void main(String[] args) { FileOutputStream fos = null ; // Declare outside try, and close try in finally { fos = new FileOutputStream("abc"+File.separator+"ai.txt"); fos.write("aiqing".getBytes()); } catch(IOException e){ } finally { if (fos != null ){ // Before releasing resources, judge whether it is empty try { fos.close(); } catch(IOException e){ throw new RuntimeException(e); } } } }
###########################################Exercise
to get a specific folder Collection of files: filters are used
public static void main(String[] args) { File f = new File("f:"+File.separator+"eclipse_javaCode"+File.separator+ "day21"+File.separator+"day19"); // System.out.println(f); List<File> list = getList(f,".java"); for(File file:list){ System.out.println(file); } } private static List<File> getList(File dir, String string) { List<File> list = new ArrayList<File>(); FileFilter filter = new FileFilterByEnding(string); getFileList(dir,list,filter); return list; } private static void getFileList(File dir, List<File> list, FileFilter filter) { // Get the file array File[] files = dir.listFiles(); // Traverse the file array for (File file:files){ if (file.isDirectory()){ getFileList(file, list, filter); } else{ if(filter.accept(file)){ list.add(file); } } } }
public class FileFilterByEnding implements FileFilter { private String suffix; public FileFilterByEnding(String suffix) { super(); this.suffix = suffix; } @Override public boolean accept(File pathname) { return pathname.getName().endsWith(suffix); } }