黑马程序员—IO输入与输出笔记

来源:互联网 发布:心意电脑闹钟软件 编辑:程序博客网 时间:2024/04/29 21:48

1.文件操作类
1)File类定义:
File类:是IO包中唯一代表磁盘文件本身信息的类,而不是文
件中的内容。
File类定义了一些与平台无关的方法来操纵文件,比如: 创
建文件、删除文件、复制文件等等。
File类 File类提供了一些方法可以用来操作文件和获得文件
信息,对于目录,java把它当做一种特殊的文件,即文件名的
列表。通过File类的方法,可以得到文件或目录,创建临时文
件,改变文件名,删除文件,列出一个目录中所有的文件或莫
个模式相匹配的文件等操作。
下面是创建一个新文件对象的多种方法:
File f1=new File("data.txt");
File f2=new File("//mydir,data.txt");
File dir=new File("//etc");
File f3=new File("dir,data.txt");
其中指定文件名创建f1,指定文件名和目录名创建f2,指定目
录名创建dir,最后则以目录对象dir和文件名创建f3。表示文
件路径时,使用转义的反斜线作为分隔符,即"//"代替"/",以
"//"开头的路径名表示绝对路径,否则表示相对路径。
构造方法:
public File(String path);
public File(String path,String name);
public File(File parent,String chile);
路径问题: java可以正确处理不同系统下的路径分隔符
在不同的操作系统上,分隔符是不一样的,在Unix下的路径分
隔符为(/),
在Dos下的路径
分隔符为(/)
其他方法:
(1)访问文件对象
public String getName() //返回文件对象名,
不包含路径名
public String getPath() //返回相对路径名
,包含文件名
public String getAbsolutePath() //返回绝
对路径名,包含文件名
public String getParent() //返回父文件
对象的路径名
public File getParentFile() //返回父文
件对象

2)forExample:要完成的功能:判断某个文件是否存在,存
在则删除,不存在则创建。

public class FileTest{
public static void mian(String [] args){
//创建对象
File file = new File("aa.txt");
if(file.exists()){
//删除
file.delete();
}else{
//创建
file.createNewFile();
}
System.out.println(file.getName);
System.out.println(file.getPath);
}
}
运行FileTest的file.delete()语句中,删除当前文
件"aa.txt" ,运行file.createNewFile(),创建文件。

2.RandomAccessFile类可以对文件进行随机读写操作。提供
了众多的文件访问方法。支持"随机访问"方式,且仅限于操
作文件,并且在随机(相对顺序而言)读写等长记录格式的文件
时有很大的优势。此类仅限于操作文件:不能访问其他的io设
备,如网络,内存映射等。
创建一个RandomAccessFile对象
RandomAccessFile(File file,String mode) ;
RandomAccessFile(String name,String mode)

两种构造方法:new RandomAccessFile(f,"rw");//读写方

new RandomAccessFile(f,"r");//只读方式
读写数据的常用方法:
读、写基本数据类型: readInt()、writeInt(int n)等;
读、写UTF字符串: readUTF()、writeUTF(String str);
读取文件中的一行: readLine();
forExample: 往文件里写三名员工的信息,第个员工含有姓
名和年龄两个属性,然后按照第二名,第一名, 第三名 的先
后顺序读出员工信息。

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
//定义了一个Employee类
public class Employee {
//初始化变量
public String name=null;

public int age=0;

//public static final int LEN = 8;

public Employee(String na, int ag){


this.name=na;
this.age=ag;
}
//定义了一个RandomFileTest类
public class RandomFileTest{
public static void main(String[] args) throws
IOException{
Employee emp1 =new
Employee("zhangsan",23);
Employee emp2 =new
Employee("lisi",24);
Employee emp3 =new
Employee("wangwu",25);

RandomAccessFile raf = new
RandomAccessFile("aa.txt","rw");

raf.write
(emp1.name.getBytes());

raf.write(emp1.age);

raf.write(emp2.name.getBytes());

raf.write(emp2.age);

raf.write(emp3.name.getBytes
());

raf.write(emp3.age);
raf.close();

int len=0;
byte[] buf =new byte[8];
RandomAccessFile raf = new
RandomAccessFile("aa.txt","r");
raf.skipBytes(12);
len=raf.read(buf);
String strName = new
String(buf,0,len);
System.out.println
(strName+":"++raf.read());

raf.seek(0);
len=raf.read(buf);
strName = new String(buf,0,len)
System.out.println(strName
+":"++raf.read());

raf.skipBytes(12);
len=rand.read(buf);
strName = new String(buf,0,len);
System.out.println(strName+":"+
+raf.read());

raf.close();
}
}
}
运行结果为:lisi:24
zhangsan :23
wangwu:25
3.各种节点流类
1)理解流的概念
流是字节序列的抽象概念。还可以理解为:是同一台计算
机或网络中不同计算机之间有序运动的数据序列。
数据序列:可以是原始的二进制字节数据,也可以是经过
特定编码处理的有格式的数据
文件是数据的静态存储形式,而流是指数据传输时的形态

它包括:字节输入流类InputStream和字节输出流
OutputStream
文件字节输入流FileInputStream类和文件字节输出
流FileOutputStream类
Reader类和Writer类
PipedInputStream类和PipedInputStream类
ByteArrayInputStream类ByteArrayOutputStream类
InputStream类和OutputStream类
InputStream类用来描述所有输入流的抽象概念
InputStream类中的方法:
int read()

int read(byte[] b)
int read(byte[] b,int off,int len)
long skip(long n)

int available()

void reset()
OutputStream类用来描述所有输出流的抽象概念
输出数据的方法:
void write(int b) throws IOException ;
void write(byte[] b) throws IOException ;
void write(byte[] b,int off,int len) throws
IOException ;
关闭输出流:
public void close() throws IOException;
清空缓冲区:
public void flush() throws IOException;

3)FileInputStream类和FileOutputStream类
FileInputStream用于顺序访问本地文件。
它从父类InputStream中继承read()、close()等方
法对本机上的文件进行操作,但不支持mark()方法和reset
()方法。

构造方法
public FileInputStream(string name) throws
FileNotFoundException
public FileInputStream(File file) throws
FileNotFoundException
读取字节的方法
public int read() throws IOException
public int read(byte[] b)throws IOException
public int read(byte[] b,int off,int len)
throws IOException

FileOutputStream类用于向一个文件写数据。它从超类
OutputStream中继承write()、close()等方法。
构造方法
public FileOutputStream(String name) throws
FileNotFoundException
public FileOutputStream(File file) throws
FileNotFoundException
public FileOutput.Stream (String name,
boolean append) throws FileNotFoundException
写入字节的方法
public void write(int b) throws
IOException
public void write(byte[] b) throws
IOException
public void write(byte[] b,int off,int
len) throws IOException

例题:打开文件
import java.io.*;
public class ReadFileTest
{
public static void main(String[] args) throws
IOException
{
try
{
//创建文件输入流对象
FileInputStream fis =new
FileInputStream("ReadFileTest.java");

int n=fis.available();
byte b[]=new byte[n];
//读取输入流
while((fis.read(b,0,n))!=-1)
{
System.out.print(new String(b));

}
System.out.println();
//关闭输入流
fis.close();
}
catch(IOException ioe)
{
System.out.println(ioe.getMessage());
}
4)Reader类与Writer类

Reader类与Writer类 是所有字符流类的抽象基类,用于简化对字符串的输入输出编程

用法:和FileInputStream类和FileOutputStream
类 都差不多
5)PipedInputStream类与PipdeOutputStream类
PipedInputStream类与PipdeOutputStream类用于在
应用程序中的创建管理通信
6)ByteArrayInputStream类与ByteArrayOutputStream

ByteArrayInputStream类与ByteArrayOutputStream
类用于以IO流方式来完成对字节
数组内容的读写, 来支持类似内存虚
拟文件
编程举例: 编写一个把输入流中所有英文字母变成大写字母
,然后将结果写入到一个输出流对象。
用这个函数来将一个字符串中的所有字符转换成
大写的。
forExample:
InputStream类和OutputStream类
InputStream类用来描述所有输入流的抽象概念
InputStream类中的方法:
int read()

int read(byte[] b)
int read(byte[] b,int off,int len)
long skip(long n)

int available()

void reset()
OutputStream类用来描述所有输出流的抽象概念
输出数据的方法:
void write(int b) throws IOException ;
void write(byte[] b) throws IOException ;
void write(byte[] b,int off,int len) throws IOException ;
关闭输出流:
public void close() throws IOException;
清空缓冲区:
public void flush() throws IOException;

3)FileInputStream类和FileOutputStream类
FileInputStream用于顺序访问本地文件。
它从父类InputStream中继承read()、close()等方法对本机上的文件进行操作,但不支持mark()方法和reset()方法。

构造方法
public FileInputStream(string name) throws FileNotFoundException
public FileInputStream(File file) throws FileNotFoundException
读取字节的方法
public int read() throws IOException
public int read(byte[] b)throws IOException
public int read(byte[] b,int off,int len) throws IOException

FileOutputStream类用于向一个文件写数据。它从超类OutputStream中继承write()、close()等方法。
构造方法
public FileOutputStream(String name) throws FileNotFoundException
public FileOutputStream(File file) throws FileNotFoundException
public FileOutput.Stream (String name,boolean append) throws FileNotFoundException
写入字节的方法
public void write(int b) throws IOException
public void write(byte[] b) throws IOException
public void write(byte[] b,int off,int len) throws IOException

例题:打开文件
import java.io.*;
public class ReadFileTest
{
public static void main(String[] args) throws IOException
{
try
{
//创建文件输入流对象
FileInputStream fis =new FileInputStream("ReadFileTest.java");
int n=fis.available();
byte b[]=new byte[n];
//读取输入流
while((fis.read(b,0,n))!=-1)
{
System.out.print(new String(b));
}
System.out.println();
//关闭输入流
fis.close();
}
catch(IOException ioe)
{
System.out.println(ioe.getMessage());
}
4)Reader类与Writer类

Reader类与Writer类 是所有字符流类的抽象基类,用于简化对字符串的输入输出编程

用法:和FileInputStream类和FileOutputStream类 都差不多
5)PipedInputStream类与PipdeOutputStream类
PipedInputStream类与PipdeOutputStream类用于在应用程序中的创建管理通信
6)ByteArrayInputStream类与ByteArrayOutputStream类
ByteArrayInputStream类与ByteArrayOutputStream类用于以IO流方式来完成对字节
数组内容的读写, 来支持类似内存虚拟文件
编程举例: 编写一个把输入流中所有英文字母变成大写字母,然后将结果写入到一个输出流对象。
用这个函数来将一个字符串中的所有字符转换成大写的。

import java.io.*;
public class ByteArrayTest {

public static void main(String[] args) throws IOException{
//定义一个字符串
String tmp = "abcdefghijklmnaefd";
//定义一个字节数组,转成字节
byte[]src = tmp.getBytes();
//创建ByteArrayInputStream 输入流对象
ByteArrayInputStream input = new ByteArrayInputStream(src);
//创建ByteArrayOutputputStream 输出流对象
ByteArrayOutputStream output =new ByteArrayOutputStream();
//调用方法
transform(input,output);

byte[] result = output.toByteArray();

System.out.print(new String(result));

}
public static void transform(InputStream in,OutputStream out) throws IOException{

int ch =0;
while((ch=in.read())!=-1){
int upperCh =Character.toUpperCase((char)ch);

out.write(upperCh);
}
}
}

总结:Java的标准数据流
标准的输入,对象是键盘。
标准的输出,对象是显示器屏幕。
标准的错误输出,对象也是显示器屏幕。
Java通过系统类System实现标准的输入输出功能。System类在java.lang包中,声明为一个final类System.in连接到键盘,是InputStream类型的实例对象,System.out连接到显示器,是PrintStream类的实例对象。不管各种底层物理设备用什么方式实现数据的终止点,InputStream的read方法总是返回-1来表示输入流的结束。在Windows下,按下Ctrl+Z组合键可以产生键盘输入流的结束标记,在Linux下,按下Ctrl+D组合

 

 

原创粉丝点击