/** * */ package com.javaIO.test; import java.io.*; /** * @author zhangbo * 2017Äê2ÔÂ15ÈÕ ÏÂÎç12:57:26 * */ public class TestMain { /* * * FileÀ๲ÌṩÁËÈý¸ö²»Í¬µÄ¹¹Ô캯Êý£¬ÒÔ²»Í¬µÄ²ÎÊýÐÎʽÁé»îµØ½ÓÊÕÎļþºÍĿ¼ÃûÐÅÏ¢¡£¹¹Ô캯Êý£º 1£©File (String pathname) Àý:File f1=new File("FileTest1.txt"); //´´½¨Îļþ¶ÔÏóf1£¬f1ËùÖ¸µÄÎļþÊÇÔÚµ±Ç°Ä¿Â¼Ï´´½¨µÄFileTest1.txt 2£©File (String parent , String child) Àý:File f2=new File(¡°D:\\dir1","FileTest2.txt") ;// ×¢Ò⣺D:\\dir1Ŀ¼ÊÂÏȱØÐë´æÔÚ£¬·ñÔòÒì³£ 3£©File (File parent , String child) Àý:File f4=new File("\\dir3"); File f5=new File(f4,"FileTest5.txt"); //ÔÚÈç¹û \\dir3Ŀ¼²»´æÔÚʹÓÃf4.mkdir()ÏÈ´´½¨ Ò»¸ö¶ÔÓ¦ÓÚij´ÅÅÌÎļþ»òĿ¼µÄFile¶ÔÏóÒ»¾´´½¨£¬ ¾Í¿ÉÒÔͨ¹ýµ÷ÓÃËüµÄ·½·¨À´»ñµÃÎļþ»òĿ¼µÄÊôÐÔ¡£ 1£©public boolean exists( ) ÅжÏÎļþ»òĿ¼ÊÇ·ñ´æÔÚ 2£©public boolean isFile( ) ÅжÏÊÇÎļþ»¹ÊÇĿ¼ 3£©public boolean isDirectory( ) ÅжÏÊÇÎļþ»¹ÊÇĿ¼ 4£©public String getName( ) ·µ»ØÎļþÃû»òĿ¼Ãû 5£©public String getPath( ) ·µ»ØÎļþ»òĿ¼µÄ·¾¶¡£ 6£©public long length( ) »ñÈ¡ÎļþµÄ³¤¶È 7£©public String[ ] list ( ) ½«Ä¿Â¼ÖÐËùÓÐÎļþÃû±£´æÔÚ×Ö·û´®Êý×éÖзµ»Ø¡£ FileÀàÖл¹¶¨ÒåÁËһЩ¶ÔÎļþ»òĿ¼½øÐйÜÀí¡¢²Ù×÷µÄ·½·¨£¬³£Óõķ½·¨ÓУº 1£© public boolean renameTo( File newFile ); ÖØÃüÃûÎļþ 2£© public void delete( ); ɾ³ýÎļþ 3£© public boolean mkdir( ); ´´½¨Ä¿Â¼ */ private static void fileTest() { //ÔÚÖ¸¶¨µÄ·¾¶Ï´´½¨Îļþ£¬Í³¼ÆÎļþÎļþ¼ÐϵÄËùÓÐÎļþÃû³Æ File dir = new File("D:\\github\\javaIOExample\\files"); File f1 = new File(dir, "fileOne.txt"); File f2 = new File(dir, "fileTwo.java"); try { if (!dir.exists()) dir.mkdir(); if (!f1.exists()) f1.createNewFile(); if (!f2.exists()) f2.createNewFile(); System.out.println("f1's AbsolutePath= " + f1.getAbsolutePath()); System.out.println("f1 Canread=" + f1.canRead()); System.out.println("f1's len= " + f1.length()); String[] FL; int count = 0; FL = dir.list(); for (int i = 0; i < FL.length; i++) { count++; System.out.println(FL[i] + "is in /rootpath"); } System.out.println("there are" + count + " file in /rootpath "); } catch (Exception e) { e.printStackTrace(); } /* * ˵Ã÷£ºFileÀàµÄ·½·¨: (1) exists()²âÊÔ´ÅÅÌÖÐÖ¸¶¨µÄÎļþ»òĿ¼ÊÇ·ñ´æÔÚ (2) mkdir()´´½¨Îļþ¶ÔÏóÖ¸¶¨µÄĿ¼£¨µ¥²ãĿ¼£© (3) createNewFile()´´½¨Îļþ¶ÔÏóÖ¸¶¨µÄÎļþ (4) list()·µ»ØÄ¿Â¼ÖÐËùÓÐÎļþÃû×Ö·û´® * */ } /* * ÒÔÎļþ×÷ΪÊý¾ÝÊäÈëÔ´µÄÊý¾ÝÁ÷¡£ * ÒÔÊý¾ÝÁ÷µÄ·½Ê½¶ÁÈ¡Îļþ */ private static void fileInputStreamTest() { try { File f = new File("src/com/javaIO/test/TestMain.java"); //Ïà¶Ô·¾¶ FileInputStream rf = new FileInputStream(f); int n = 512; byte buffer[] = new byte[n]; while ((rf.read(buffer, 0, n) != -1) && (n > 0)) { System.out.println(new String(buffer)); } System.out.println(); rf.close(); } catch (Exception e) { e.printStackTrace(); } } //±ê×¼Êä³öÁ÷ //°ÑÊý¾Ýдµ½ write.txt ÎļþÖÐ private static void fileOutputStreamTest(){ try { System.out.println("please Input from Keyboard"); int count, n = 512; // byte buffer[] = new byte[n]; // count = System.in.read(buffer); FileInputStream rf = new FileInputStream(new File("src/com/javaIO/test/TestMain.java")); byte buffer[] = new byte[n]; FileOutputStream wf = new FileOutputStream("D:\\github\\javaIOExample\\files\\write.txt"); while ((rf.read(buffer, 0, n) != -1) && (n > 0)) { // System.out.println(new String(buffer)); wf.write(buffer, 0, n); } wf.close(); // µ±Á÷д²Ù×÷½áÊøÊ±£¬µ÷ÓÃclose·½·¨¹Ø±ÕÁ÷¡£ System.out.println("Save to the write.txt"); } catch (IOException IOe) { System.out.println("File Write Error!"); } } // FileInputStreamÁ÷ºÍFileOutputStreamµÄÓ¦Óà private static void fileinoutputTest(){ try { File inFile = new File("src/com/javaIO/test/TestMain.java"); File outFile = new File("D:\\github\\javaIOExample\\files\\TestMaincopy2.java"); FileInputStream finS = new FileInputStream(inFile); FileOutputStream foutS = new FileOutputStream(outFile); int c; while ((c = finS.read()) != -1) { foutS.write(c); } finS.close(); foutS.close(); } catch (IOException e) { System.err.println("FileStreamsTest: " + e); } } //½«BufferedReaderÓë±ê×¼µÄÊý¾ÝÁ÷Ïà½Ó private static void readWriteToFile() throws IOException{ InputStreamReader sin = new InputStreamReader(System.in); BufferedReader bin = new BufferedReader(sin); FileWriter out = new FileWriter("myfile.txt"); BufferedWriter bout = new BufferedWriter(out); String s; while ((s = bin.readLine()).length() > 0) { bout.write(s, 0, s.length()); } } /* * InputStreamºÍOutputStreamÀà´¦ÀíµÄÊÇ×Ö½ÚÁ÷£¬Êý¾ÝÁ÷ÖеÄ×îСµ¥Î»ÊÇ×Ö½Ú(8¸öbit) ReaderÓëWriter´¦ÀíµÄÊÇ×Ö·ûÁ÷£¬ÔÚ´¦Àí×Ö·ûÁ÷Ê±Éæ¼°ÁË×Ö·û±àÂëµÄת»»ÎÊÌâ */ private static void readBuff(byte[] buff) throws IOException { ByteArrayInputStream in = new ByteArrayInputStream(buff); int data; while ((data = in.read()) != -1) System.out.print(data + " "); System.out.println(); in.close(); } private static void encodeTest() { try { System.out.println("ÄÚ´æÖвÉÓÃunicode×Ö·û±àÂ룺"); char c = 'ºÃ'; int lowBit = c & 0xFF; int highBit = (c & 0xFF00) >> 8; System.out.println("" + lowBit + " " + highBit); String s = "ºÃ"; System.out.println("±¾µØ²Ù×÷ϵͳĬÈÏ×Ö·û±àÂ룺"); readBuff(s.getBytes()); System.out.println("²ÉÓÃGBK×Ö·û±àÂ룺"); readBuff(s.getBytes("GBK")); System.out.println("²ÉÓÃUTF-8×Ö·û±àÂ룺"); readBuff(s.getBytes("UTF-8")); } catch (IOException e) { e.printStackTrace(); } } /** * @author zhangbo * 2017Äê2ÔÂ15ÈÕ ÏÂÎç12:57:28 * @throws IOException */ public static void main(String[] args) { // TODO Auto-generated method stub // fileInputStreamTest() ; // fileOutputStreamTest(); //fileinoutputTest(); encodeTest(); System.out.println("Hello World !!!"); } }