[置顶]再学java基础(7)java IO実戦【待续...】


import java.io.*;
public class TestFileInputStream {
  public static void main(String[] args) {
    int b = 0;
    FileInputStream in = null;
    try {
      in = new FileInputStream("d:\\share\\java\\io\\TestFileInputStream.java");
    } catch (FileNotFoundException e) {
      System.out.println("       "); 
      System.exit(-1);
    }
    
    try {
      long num = 0;
      while((b=in.read())!=-1){   //         。          。。  
        System.out.print((char)b); 
        num++;
      }
      in.close();  
      System.out.println();
      System.out.println("     "+num+"    ");
    } catch (IOException e1) {
      System.out.println("      "); System.exit(-1);
    }
  }
}
													import java.io.*;
public class TestFileOutputStream {
  public static void main(String[] args) {
	  int b = 0;
	  FileInputStream in = null;
	  FileOutputStream out = null;
	  try {
	    in = new FileInputStream("d:/share/java/HelloWorld.java");  //      ,          
	    out = new FileOutputStream("d:/share/java/io/HW.java"); //                              FIleOutputStream          。
	    while((b=in.read())!=-1){
	      out.write(b);
	    }
	    in.close(); 
	    out.close();
																							  }	catch (FileNotFoundException e2) {
																								System.out.println("       "); System.exit(-1);
	  } catch (IOException e1) {
	    System.out.println("      "); System.exit(-1);
	  }
	  System.out.println("     ");
  }
}
import java.io.*;
public class TestBufferStream1 {
  public static void main(String[] args) {
    try {
      FileInputStream fis = 
              new FileInputStream("d:\\share\\java\\HelloWorld.java"); //                
      BufferedInputStream bis = 
              new BufferedInputStream(fis);
      int c = 0;
      System.out.println(bis.read());
      System.out.println(bis.read());
      bis.mark(100); //        100,    100    。
      for(int i=0;i<=10 && (c=bis.read())!=-1;i++){
        System.out.print((char)c+" ");
      }
      System.out.println(); 
      bis.reset(); //     100     。
      for(int i=0;i<=10 && (c=bis.read())!=-1;i++){
        System.out.print((char)c+" ");
      }
      bis.close();
    } catch (IOException e) {e.printStackTrace();}
  }
}
import java.io.*;
public class TestBufferStream2 {
  public static void main(String[] args) {
    try {
	//  new FileWriter("d:\\share\\java\\dat2.txt") //         
      BufferedWriter bw = new BufferedWriter(new FileWriter("d:\\share\\java\\dat2.txt"));
      BufferedReader br = new BufferedReader(
             new FileReader("d:\\share\\java\\dat2.txt")); //            。
      String s = null;
      for(int i=1;i<=100;i++){
        s = String.valueOf(Math.random());
        bw.write(s); //       。
        bw.newLine();
      }
      bw.flush();
      while((s=br.readLine())!=null){
        System.out.println(s);
      }
      bw.close(); 
      br.close();
    } catch (IOException e) { e.printStackTrace();}
  }
}
import java.io.*;
public class TestFileWriter {
  public static void main(String[] args) {
    FileWriter fw = null;
    try {
      fw = new FileWriter("d:\\bak\\unicode.dat"); //        。
      for(int c=0;c<=50000;c++){
        fw.write(c);
      }
      fw.close();
    } catch (IOException e1) {
    	e1.printStackTrace();
      System.out.println("      ");
      System.exit(-1);
    }
  }
}
import java.io.*;

public class TestFileWriter2 {
	public static void main(String[] args) throws Exception {
		FileReader fr = new FileReader("d:/java/io/TestFileWriter2.java"); 
		FileWriter fw = new FileWriter("d:/java/io/TestFileWriter2.bak");
		int b;
		while((b = fr.read()) != -1) {
			fw.write(b);
		}
		fr.close();
		fw.close();
	}
}
import java.io.*;
public class TestFileReader {
  public static void main(String[] args) {
    FileReader fr = null; 
    int c = 0;
    try {
      fr = new FileReader("d:\\share\\java\\io\\TestFileReader.java"); //             。    
      int ln = 0;
      while ((c = fr.read()) != -1) {
        //char ch = (char) fr.read();
        System.out.print((char)c);
        //if (++ln >= 100) { System.out.println(); ln = 0;}
      }
      fr.close();
    } catch (FileNotFoundException e) {
      System.out.println("       ");
    } catch (IOException e) {
      System.out.println("      ");
    }

  }
}
import java.io.*;
public class TestDataStream {
  public static void main(String[] args) {
    ByteArrayOutputStream baos = 
                        new ByteArrayOutputStream(); //        
    DataOutputStream dos = 
                        new DataOutputStream(baos); //        。
    try {
      dos.writeDouble(Math.random());
      dos.writeBoolean(true);
      ByteArrayInputStream bais = 
          new ByteArrayInputStream(baos.toByteArray());
      System.out.println(bais.available());
      DataInputStream dis = new DataInputStream(bais);
      System.out.println(dis.readDouble());
      System.out.println(dis.readBoolean());
      dos.close();  dis.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

1、書き込みデータ:
/**
	 *    
	 * 
	 * @param path
	 *                   
	 * @param dataList
	 *              TXT     
	 * @throws IOException
	 */
	public void writeTXT(String path, List<String> dataList) throws IOException {

		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
				new FileOutputStream(path, true)));

		for (String string : dataList) {
			bw.write(string);
			bw.newLine();
		}
		bw.flush();
		bw.close();
		bw = null;
	}

2、ファイルのコピー
/**
	 *     
	 * 
	 * @param fromPath
	 *               
	 * @param toPath
	 *                
	 */
	public  void copyFile(String fromPath, String toPath) {
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		System.out.println("copyFile start---   :" + fromPath + ",    :"
				+ toPath);
		try {
			bis = new BufferedInputStream(new FileInputStream(fromPath));
			bos = new BufferedOutputStream(new FileOutputStream(toPath));
			byte b[] = new byte[1024 * 8];
			int i;

			while ((i = bis.read(b)) > -1) {
				bos.write(b, 0, i);
			}
			bos.flush();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (bis != null)
					bis.close();
				if (bos != null)
					bos.close();

			} catch (IOException e) {
				e.printStackTrace();
				bis = null;
				bos = null;
			}
		}
	}

3,読み込んだpropertiesパス
/**
	 * 
	 * @param propertiesPath
	 *                 properties  
	 * @param propertiesKey
	 *            properties  key
	 * @return properties  value
	 * @throws IOException
	 */
	public  String getPropertiesValue(String propertiesPath,
			String propertiesKey) throws IOException {

		String path = PropertiesUtil.loadProperties(propertiesPath)
				.getProperty(propertiesKey);
		//              
		File file;
		file = new File(path);
		if (!file.exists()) {
			file.mkdirs();
		}
		file = null;
		//            
		file = new File(path);
		if (!file.exists()) {
			file.mkdirs();
		}
		file = null;
		return PropertiesUtil.loadProperties(propertiesPath).getProperty(
				propertiesKey);
	}