重点:1、 IO框架介绍。 (Input/Output)
2、File类及使用 表示磁盘上的文件或目录、 File类,查看API文档,有好多方法熟悉。
eg:delete() getName() getParent() createNewFile()
import java.io.File;
import java.io.IOException;
public class FileDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// File file=new File("C:\\Program Files\\Common Files");
// String fileaddress=file.getParent();
// System.out.println( fileaddress);
// System.out.println(file.getName());
// System.out.println(file.isDirectory());
System. out.println((new File("C:\\Program Files\\Common
Files")).getName());
File file= new File("E:\\tian" );
file.mkdir();
File file1= new File("E:\\tian\\tao.txt" );
try {
file1.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
3、 递归算法:程序调用自身的编程技巧。
必须有一个明确的停止条件,
eg:阶乘:public class Digui {
public static void main(String[] args) {
System. out.println(jie.jieche(5));}
}
class jie{
public static int jieche(int number){
if(number==1){
return 1;
} else{
return number*jieche(number-1);
} } }
4、流的概念和分类
流:当程序需要读取数据的时候,就会开启一个通向数据源的流,这个数据源可以是文件,内存,或者是
网络链接,写入数据的时候,就会开启一个通向目的地的流,数据就会像“流”动一样。
流的分类:输入/输出流
字节流 :用来读写8位二进制的字节
InputStream/OutputStream FileInputStream/FileOutputStream
主要关注:FileInputSstream的read方法,和FileOutputStream中个write方法,
eg:
public class FileInputStreamDemo {
public static void main(String[] args) throws IOException {
Stream. copy(new File( "D:\\1.jpg"), new File("E:\\2.jpg" ));
}
}
class Stream{
public static void copy(File copyfile,File cutfile) throws IOException{
FileInputStream file1= new FileInputStream(copyfile);
FileOutputStream file2= new FileOutputStream(cutfile);
byte [] by=new byte[1024*1024];
int len=0;
while((len=file1.read(by))!=-1){
file2.write(by,0,len);
}
file1.close();
file2.close();
}
}
ByteArrayInputStream :字节数组输入流
ByteArrayOutputStream:字节数组输出流。
这两个类中都把字节数组为源和目的地 (字符串转化为字节数组方法getBytes())
构造方法:ByteArrayInputStream(byte array[])
ByteArrayInputStream( byte array[] ,int start ,int numbytes)
主要是数组的操作。
节点流:用于直接操作目标设备的流
过滤流(包装流):是对一个已存在的流的链接和封装,通过对数据的处理为程序提供功能强大、灵活的
读写功能。
为输入输出流增加缓存,BufferedInputstream/BufferedOutputStream
DataInputStream/DataOutputStream读取基本的数据类型,
(注意数组读写顺序必须一致)
字节流提供了任意类型输入/输出操作的足够功能,但不能直接操作Unicode字符,因而需要字符流
字符流:用来读写16位二进制的字符 ,读取文本文件的时候一般用字符流。
顶层的抽象类Reader和Writer (记得用flush函数清空缓存)
子类,FileReader/FileWriter
public class FileReaderWriterDemo {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
FileReader file1= new FileReader(“E:\1.txt” );
FileWriter file2= new FileWriter(“E:\2.txt” );
char [] buf=new char[100000];
int len=0;//实际读到的字符个数。
while((len=file1.read(buf))!=-1){
file2.write(buf, 0, len);
}
file1.close();
file2.close();
}
}
IO流:
IO框架介绍:
IO(Input\Output)是计算机输入和输出的接口,Java的核心库Java.io提供了全方位的IO接口,包括:文件系统的操作,文件读写,标准设备输出等等。
file: 文件和目录类。
inputStream/outputStream:字节流读写类。
Reader/writer:字符流读写类。
File类及使用:
一个file类的对象,表示磁盘上的文件或目录。
file类提供了与平台无关的方法来对磁盘上的文件或目录进行操作。
file类直接处理文件和文件系统,比如删除文件,获取文件的大小等。
file类没有提供方法从文件读取和向文件存储信息。
file类提供了很多方法来实现文件的操作和获得文件的属性。getname,等。
方法list 和listFile 一个得到String数组 一个得到File数组,
eg:返回以.java结尾的文件的路径名,或文件,放到String数组中。
String [] files = file.list(new FilenameFilter(){
public bolean accept (File dir,Strign name){
return name.endsWith(".java");
}
});
返回以.java文件为结尾的File对象数组。
File [] files = file.listFile(new FilenameFilter(){
public boolean accept(File dir ,String name){
return name.endsWith(".java");
}
});
递归算法:
程序调用自身的编程技巧,为递归。
递归过程就是函数调用自身。‘
在递归时要有明确的递归结束条件,
流的概念和分类:
流是当程序读取数据时,启动一个通向数据源的流,这个数据源可以是文件,内存或是网络连接,类似,当写入数据时,就会开启一个通向目的地的流,这个时候数据就会像流一样,流动。
流的分类:
按流的方向:输入流、输出流。
按流的传输单位: 字节流、、和字符流、
字节流:用来读写8位二进制字节。
字节流: 用来读写16位二进制字符。
按流的功能分:节点流、、过滤流。
节点流:直接操作目标设备的流。
过滤流:对一个已经存在的流的连接和封装,通过对数据进行处理为程序提供功能强大、灵活的读写功能。
字节流:
InputStream抽象类 OutputStream抽象类
字节流顶层抽象类:
用于向字节流读写8位二进制字节,一般用于读写图像或声音等二进制文件。
各种方法:read write。
FileInputStream和FileOutputStream
FileInputStream 类表示能从文件读取字节的InputStream类。
FileOutputStream表示能向文件写入字节的OutputStream类。
ByteArrayInputStream和ByteArrayOutputStream
操作参考API,好多方法都是继承与inputstream
过滤流:
仅仅为底层透明地提供扩展功能的输入 输出流的包装,这些流一般由普通类的方法访问。
过滤字节流FilterInputstream和FilterOutputStream
BufferInput/outputStream和DataInput/OutputStream
转换流:
InputStreamReader和OutputStreamWriter
转换流指字节流与自负流之间的转换。
转换流方便了文件的读写,他是两种流可以自由转换,提高了程序的灵活性。字符流的底层仍是字节流, 字符流转成字节流操作更高效,
使用非默认编码读写文件时,要用到转换流,字节流的重载构造方法提供了指定了编码格式参数,而字符流FileReader和FileWriter是默认编码的文本文件,
构造函数就可以指定编码格式,其实非常简单,就是在平常读取写入文件时,在创建流的时候要利用转换流类的构造方法指定编码格式。
//建立一个读取字符BufferedReader的类的实例,用于读取文件。读取的文件的编码为GBK
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("D:\\test.txt"),"GBK"));
//建立一个写入对象,把读的到的字符写入到对象文件中,字符编码为UTF-8
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D:\\test2.txt"),"UTF-8"));
//定义读到的字符串变量,
String str = null;
//读取文件中的字符,利用while循环,按行读取,
while ((str=br.readLine())!=null) {
//write函数把字符写入到指定的目录文件中,字符“\r\n”到文件中为换行。
bw.write(str+"\r\n");
System.out.println(str);
}