您的位置:首页 > 编程语言 > Java开发

Java IO学习笔记

2013-01-05 11:03 302 查看
Java不会,就去学Android,简直是扯淡!后悔晚了,奋起直追吧。

File类;RandomAccessFile;OutputStream,InputStream,字节流;Writer,Reader字符流,四个流类。

字节流:InputStream, OutputStream

字符流:Writer, Reader

这4个都是抽象类。字节流和字符流的区别:字符流使用了缓存,字节流没有。

开发中使用字节流好呢?字符流好呢?

R:当然是字节流好了,01010101计算机的母语。

整个io包中字节输出流的最大父类:OutputStream

1.File类(文件的创建、删除、目录的创建等)

import java.io.File;
import java.io.IOException;
public class FileDemo04
{
	public static void main(String[] args) 
	{
		File f=new File("g:"+File.separator+"test.txt");//separator为了通用因不同系统的分隔符不同win\,linux/
		if(f.exists()){//如果文件存在
			f.delete();//删除文件
		}else{//不存在
			try{
				f.createNewFile();//则创建一个新文件
			}catch(IOException e){
				e.printStackTrace();//异常信息
			}
		}
	}
}


3.OutputStream

void write(byte[] b)

import java.io.File;
import java.io.OutputStream;
import java.io.FileOutputStream;
/**void write(byte[] b)*/
public class OutputStreamDemo01{
	public static void main(String args[]) throws Exception{//异常直接抛出,不处理
		//第一步、使用File类找到一个文件
		File f=new File("g:"+File.separator+"test.txt");
		//第二步、通过子类实例化父对象
		OutputStream out=new FileOutputStream(f);//通过对象多态性,进行实例化
		//第三步、进行写操作
		String str="today ok or not?!!!";//准备一个字符串
		byte[] b=str.getBytes();			//只能输出byte数组,所以将字符串变为byte数组
		out.write(b);						//将内容输出保存为文件
		//第四步、关闭输出流
		out.close();
	}
}

void write(int b)

import java.io.File;
import java.io.OutputStream;
import java.io.FileOutputStream;
/**void write(int b)*/
public class OutputStreamDemo02{
	public static void main(String args[]) throws Exception{//异常直接抛出,不处理
		//第一步、使用File类找到一个文件
		File f=new File("g:"+File.separator+"test.txt");
		//第二步、通过子类实例化父对象
		OutputStream out=new FileOutputStream(f);//通过对象多态性,进行实例化
		//第三步、进行写操作
		String str="what is wrong?!";		//准备一个字符串
		byte[] b=str.getBytes();			//只能输出byte数组,所以将字符串变为byte数组
		for(int i=0;i<b.length;i++){
			out.write(b[i]);				//每次写入一个字节
		}
		//第四步、关闭输出流
		out.close();
	}
}

FileOutputStream(File f,boolean append)向文件末尾追加内容

import java.io.File;
import java.io.OutputStream;
import java.io.FileOutputStream;

public class OutputStreamDemo03{
	public static void main(String args[]) throws Exception{//异常直接抛出,不处理
		//第一步、使用File类找到一个文件
		File f=new File("g:"+File.separator+"test.txt");
		//第二步、通过子类实例化父对象
		OutputStream out = null;
		out=new FileOutputStream(f,true);//通过对象多态性,进行实例化,第二参数为true是追加内容
		//第三步、进行写操作
		String str="\r\nwhat is wrong?!";		//准备一个字符串\r\n实现换行
		byte[] b=str.getBytes();			//只能输出byte数组,所以将字符串变为byte数组
		for(int i=0;i<b.length;i++){
			out.write(b[i]);				//每次写入一个字节
		}
		//第四步、关闭输出流
		out.close();
	}
}


Java文件操作知多少啊。

File.createTempFile(fileName, ".amr", fpath);

三个参数 :

1.前缀

2.后缀 如果此参数为null,则用.tmp代替

3.目录



java.io.InputStream

A readable source of bytes.

Most clients will use input streams that read data from the file system (
FileInputStream
), the network (
java.net.Socket.getInputStream()
/
java.net.HttpURLConnection.getInputStream()
),
or from an in-memory byte array (
ByteArrayInputStream
).

Use
InputStreamReader
to adapt a byte stream like this one into a character stream.

Most clients should wrap their input stream with
BufferedInputStream
. Callers that do only bulk reads may omit buffering.

Some implementations support marking a position in the input stream and resetting back to this position later. Implementations that don't return false from
markSupported()
and throw an
IOException
when
reset()
is called.

Subclassing InputStream

Subclasses that decorate another input stream should consider subclassing
FilterInputStream
, which delegates all calls to the source input stream.

All input stream subclasses should override both
read()
and
read(byte[],int,int)
. The three argument overload is necessary for bulk access to the data. This is much more efficient than byte-by-byte access.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: