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

浅谈IO流常遇到的一些问题,有关输入流和输出流的问题,InputStream和OutputStream,Reade和Writer等

2020-08-01 12:27 281 查看

IO


1、IO->Input  Output

  //构造方法

[code]	public static void test1(){
File file =
new File("c:\\a\\b\\a.txt");
File file2  =
new File("c:\\a\\b","a.txt");

File path =
new File("c:\\a\\b");
File file3 =
new File(path,"a.txt");
System.out.println(file+","+file2+","+file3);
}

//常用方法

[code]public static void test4(){
File file =  new File("a.txt");  //创建一个新文件
System.out.println(file.isFile());  //判断是否为一个文件
System.out.println(file.isDirectory());  //判断是否为一个文件夹
System.out.println(file.getName());  //得到文件的名字
System.out.println(file.getPath());  //得到文件相对的路径
System.out.println(file.getAbsolutePath());   //得到文件的绝对路径
System.out.println(file.length());   //得到文件内容的长度
}

//文件的创建和删除

[code]public static void test21(){
//当没有指明路径时,文件被默认创建在当前工程下
File file = new File("c:\\a\\b\\c");
if(!file.exists()){
boolean b = file.mkdirs();
System.out.println("创建:"+b);
}else{
boolean b = file.delete();
System.out.println("删除:"+b);
}
}

2、按照流的读写方式

     字节流:字节流读取的时候,读到一个字节就返回一个字节,可以处理所有的类型数据。主要处理的是byte类型的数据,以byte数组为准,主要操作类是OutputStream、InputStream

     字符流:字符流使用了字节流读到一个或多个字节,先去查指定的编码表,将查到的字符返回。只能处理字符数据。java提供了Reader、Writer两个专门操作字符流的类。


3、按照流的方向

       输入流是写入数据,输出流是输出数据。

     
4、输入流的核心方法:read()       操作的数组类型
      输出流的核心方法:write()

      输入流:字节流使用InputStream,字符流使用Reader

      输出流:字符流使用OutputStream,字符流使用Writer

      转换流:使用InputStreamReader,OutputStream来关联,实际上是通过byte[]和String来关联

5、操作的数组类型
        字节流:byte类型
        字符流:char类型
6、FileInputStream和FileOutputStream
     使用步骤:
           选择流类(输入还是输出)
           确定方法(read还是write)
           关闭流   close
7、对象的输入流和输出流
      对象的序列化:把对象转换成二进制的流,写到数据文件
      对象的反序列化:把数据文件中二进制的流代表的数据,恢复为对象

8、按照是否能直接操作数据文件
        节点流:直接在构造方法中传入要读写的文件对象或文件名
        处理流:不直接在构造方法中传入要读写的文件对象或文件名

9、FileReader和FileWriter
 
10、缓冲流:BufferdReader 读取   和   BufferdWriter   \n
                     readLine()   成行读取
       打印流:PrintWriter();  
                     println();

11、转换流:InputStreamReader    OutputStreamWriter

12、输入流和输出流是一个流对象:RandomAccessFile类

[code]public class Demo3 {
public static void test1() throws IOException {
FileWriter fw=new FileWriter("fw.txt");
fw.write("hello姬楚");
fw.close();
}
public static void test2() throws IOException {
File file=new File("fw.txt");
if(!file.exists()) {
file.createNewFile();
}
FileReader fr=new FileReader(file);
char[] buf=new char[(int)file.length()];
fr.read(buf);
System.out.println(new String(buf));
}
//练习:复制fw.txt中的内容到fw1.txt文件中
public static void test3() throws IOException {

File file=new File("fw.txt");
if(!file.exists()) {
file.createNewFile();
}
FileReader fr=new FileReader(file);
char[] buf=new char[(int)file.length()];
fr.read(buf);
FileWriter fw1=new FileWriter("fw1.txt");
fw1.write(buf);
fr.close();
fw1.close();

//		fr.read(buf);
//		System.out.println(new String(buf));
}
[code]public class Demo4 {
public static void test1() throws IOException {
PrintWriter pw=new PrintWriter("pw.txt");
pw.println("张三 123456 zhangsan@tedu.cn");
pw.println("李四 123456 lisi@tedu.cn");
pw.flush();//清空
pw.close();
}

public static void test2() throws IOException {
//创建一个缓存流的输入流的对象
BufferedReader br=new BufferedReader(new FileReader("pw.txt"));
String str="";
//readLine方法:返回一行,如果返回值为null,说明文件内容读取完毕
while((str=br.readLine())!=null) {
String[] value=str.split("\\s");
String name=value[0];
String phone=value[1];
String email=value[2];
System.out.println("name:"+name+",phone:"+phone+",email:"+email);
}
br.close();

}

public static void test3() throws IOException {
InputStream ins=new FileInputStream("ins.txt");
BufferedReader br=new BufferedReader(new InputStreamReader(ins));
}

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐