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类(文件的创建、删除、目录的创建等)
3.OutputStream
void write(byte[] b)
void write(int b)
FileOutputStream(File f,boolean append)向文件末尾追加内容
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 (
or from an in-memory byte array (
Use
Most clients should wrap their input stream with
Some implementations support marking a position in the input stream and resetting back to this position later. Implementations that don't return false from
All input stream subclasses should override both
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
InputStreamReaderto 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
IOExceptionwhen
reset()is called.
Subclassing InputStream
Subclasses that decorate another input stream should consider subclassingFilterInputStream, 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.
相关文章推荐
- 传智博客学习笔记12--JAVA IO
- Java IO学习笔记(七):打印流
- Java IO学习笔记
- Java IO学习笔记:概念与原理
- Java学习笔记-IO
- java io 学习笔记(一)
- JAVA学习笔记之IO详解一
- java io学习笔记(一)
- [学习笔记]Java IO之文件
- 尚硅谷java学习笔记——NIO(New IO)
- java NIO非阻塞式IO网络编程学习笔记(一)
- [学习笔记]Java IO之文件
- Java文件IO学习笔记(五)-删除文件夹和文件
- Java IO学习笔记(三):字节流与字符流
- Java IO学习笔记(八):System类对IO的支持
- Java基础学习笔记十九 IO
- java学习笔记IO输入与输出——File类及IO其它类(4)
- Java学习笔记(IO_1)
- Java IO学习笔记(三):字节流与字符流
- Java IO 学习笔记五