자바 읽 기 쓰기 바 이 너 리 파일 작업

2500 단어 자바 기술
/**
 *        
 */
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class MainClass
{
	/**
	 * java.io   OutputStream              。
	 * FileOutputStream    ,             。
	 * DataOutputStream OutputStream      ,   
	 *      FileOutputStream ,              。
	 */
	public void writeMethod1()
	{
		String fileName="c:/kuka1.dat";
		int value0=255;
		int value1=0;
		int value2=-1;
		try
		{
			// DataOutputStream FileOutputStream            
			//FileOutputStream            kuka.dat,       ,
			//         ,                 。  FileOutputStream
			//       DataOutputStream    ,DataOutputStream    
			//         。
			DataOutputStream out=new DataOutputStream(new FileOutputStream(fileName));
			out.writeInt(value0);
			out.writeInt(value1);
			out.writeInt(value2);
			out.close();
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	//         ,     BufferedOutputStream       
	public void writeMethod2()
	{
		String fileName="c:/kuka2.txt";
		try
		{
			DataOutputStream out=new DataOutputStream(
								 new BufferedOutputStream(
								 new FileOutputStream(fileName)));
			out.writeInt(10);
			System.out.println(out.size()+" bytes have been written.");
			out.writeDouble(31.2);
			System.out.println(out.size()+" bytes have been written.");
			out.writeBytes("JAVA");
			System.out.println(out.size()+" bytes have been written.");
			out.close();
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	/**
	 *              FileInputStream,DataInputStream
	 * BufferedInputStream 。   DataOutputStream,DataInputStream
	 *                、  、  、  、   、   、
	 *    、      。
	 */
	public void readMethod1()
	{
		String fileName="c:/kuka1.dat";
		int sum=0;
		try
		{
			DataInputStream in=new DataInputStream(
							   new BufferedInputStream(
							   new FileInputStream(fileName)));
			sum+=in.readInt();
			sum+=in.readInt();
			sum+=in.readInt();
			System.out.println("The sum is:"+sum);
			in.close();
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
	public void readMethod2()
	{
		try
		{
			FileInputStream stream=new FileInputStream("c:/kuka.dat");
			int c;
			while((c=stream.read())!=-1)
			{
				System.out.println(c);
			}
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}
}

좋은 웹페이지 즐겨찾기