java IO流详解

Java中的IO流详解 Java NIO (New IO,Non-Blocking IO)是从Java 1.4版本开始引入的一套新的IO API,可以替代标准的Java IO API。NIO与原来的IO有同样的作用和目的,但是使用的方式完全不同,NIO支持面向缓冲区的(IO是面向流的)、基于通道的IO操作。NIO将以更加高效的方式进行文件的读写操作。Java API中提供了两套NIO,一套是针对标准输入输出NIO,另一套就是网络编程NIO。​ |-----FileChannel:处理本地文件。 阅读详情

学习Java IO,不得不提到的就是JavaIO流。

流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象。即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输特性将流抽象为各种类,方便更直观的进行数据操作。

IO流的分类

根据处理数据类型的不同分为:字符流和字节流

根据数据流向不同分为:输入流和输出流

字符流和字节流

字符流的由来: 因为数据编码的不同,而有了对字符进行高效操作的流对象。本质其实就是基于字节流读取时,去查了指定的码表。字节流和字符流的区别:

(1)读写单位不同:字节流以字节(8bit)为单位,字符流以字符为单位,根据码表映射字符,一次可能读多个字节。

(2)处理对象不同:字节流能处理所有类型的数据(如图片、avi等),而字符流只能处理字符类型的数据。

(3)字节流在操作的时候本身是不会用到缓冲区的,是文件本身的直接操作的;而字符流在操作的时候下后是会用到缓冲区的,是通过缓冲区来操作文件,我们将在下面验证这一点。

结论:优先选用字节流。首先因为硬盘上的所有文件都是以字节的形式进行传输或者保存的,包括图片等内容。但是字符只是在内存中才会形成的,所以在开发中,字节流使用广泛。

输入流和输出流

对输入流只能进行读操作,对输出流只能进行写操作,程序中需要根据待传输数据的不同特性而使用不同的流。

Java流类图结构:

 

Java IO流对象

1. 输入字节流InputStream

定义和结构说明:

从输入字节流的继承图可以看出:

InputStream 是所有的输入字节流的父类,它是一个抽象类。

ByteArrayInputStream、StringBufferInputStream、FileInputStream 是三种基本的介质流,它们分别从Byte 数组、StringBuffer、和本地文件中读取数据。PipedInputStream 是从与其它线程共用的管道中读取数据,与Piped 相关的知识后续单独介绍。

ObjectInputStream 和所有FilterInputStream的子类都是装饰流(装饰器模式的主角)。意思是FileInputStream类可以通过一个String路径名创建一个对象,FileInputStream(String name)。而DataInputStream必须装饰一个类才能返回一个对象,DataInputStream(InputStream in)。如下图示:

加载中...


实例操作演示:

【案例 】读取文件内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
 * 字节流
 * 读文件内容
 * */
import java.io.*;
class hello{
   public static void main(String[] args) throws IOException {
       String fileName="D:"+File.separator+"hello.txt";
       File f=new File(fileName);
       InputStream in=new FileInputStream(f);
       byte[] b=new byte[1024];
       in.read(b);
       in.close();
       System.out.println(new String(b));
    }
}

注意:该示例中由于b字节数组长度为1024,如果文件较小,则会有大量填充空格。我们可以利用in.read(b);的返回值来设计程序,如下案例:

【案例】读取文件内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
 * 字节流
 * 读文件内容
 * */
import java.io.*;
class hello{
   public static void main(String[] args) throws IOException {
       String fileName="D:"+File.separator+"hello.txt";
       File f=new File(fileName);
       InputStream in=new FileInputStream(f);
       byte[] b=new byte[1024];
       int len=in.read(b);
       in.close();
       System.out.println("读入长度为:"+len);
       System.out.println(new String(b,0,len));
    }
}

注意:观察上面的例子可以看出,我们预先申请了一个指定大小的空间,但是有时候这个空间可能太小,有时候可能太大,我们需要准确的大小,这样节省空间,那么我们可以这样做:

【案例】读取文件内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
 * 字节流
 * 读文件内容,节省空间
 * */
import java.io.*;
class hello{
   public static void main(String[] args) throws IOException {
       String fileName="D:"+File.separator+"hello.txt";
       File f=new File(fileName);
       InputStream in=new FileInputStream(f);
       byte[] b=new byte[(int)f.length()];
       in.read(b);
       System.out.println("文件长度为:"+f.length());
       in.close();
       System.out.println(new String(b));
    }
}

【案例】逐字节读

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
 * 字节流
 * 读文件内容,节省空间
 * */
import java.io.*;
class hello{
   public static void main(String[] args) throws IOException {
       String fileName="D:"+File.separator+"hello.txt";
       File f=new File(fileName);
       InputStream in=new FileInputStream(f);
       byte[] b=new byte[(int)f.length()];
       for (int i = 0; i < b.length; i++) {
           b[i]=(byte)in.read();
       }
       in.close();
       System.out.println(new String(b));
    }
}

注意:上面的几个例子都是在知道文件的内容多大,然后才展开的,有时候我们不知道文件有多大,这种情况下,我们需要判断是否独到文件的末尾。

【案例】字节流读取文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
 * 字节流
 *读文件
 * */
import java.io.*;
class hello{
   public static void main(String[] args) throws IOException {
       String fileName="D:"+File.separator+"hello.txt";
       File f=new File(fileName);
       InputStream in=new FileInputStream(f);
       byte[] b=new byte[1024];
       int count =0;
       int temp=0;
       while((temp=in.read())!=(-1)){
           b[count++]=(byte)temp;
       }
       in.close();
       System.out.println(new String(b));
    }
}

注意:当读到文件末尾的时候会返回-1.正常情况下是不会返回-1的。

【案例】DataInputStream类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
  
public class DataOutputStreamDemo{
   public static void main(String[] args) throws IOException{
       File file = new File("d:" + File.separator +"hello.txt");
       DataInputStream input = new DataInputStream(new FileInputStream(file));
       char[] ch = new char[10];
       int count = 0;
       char temp;
       while((temp = input.readChar()) != 'C'){
           ch[count++] = temp;
       }
       System.out.println(ch);
    }
}

【案例】PushBackInputStream回退流操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.PushbackInputStream;
  
/**
 * 回退流操作
 * */
public class PushBackInputStreamDemo{
    public static void main(String[] args) throwsIOException{
       String str = "hello,rollenholt";
       PushbackInputStream push = null;
       ByteArrayInputStream bat = null;
       bat = new ByteArrayInputStream(str.getBytes());
       push = new PushbackInputStream(bat);
       int temp = 0;
       while((temp = push.read()) != -1){
           if(temp == ','){
                push.unread(temp);
                temp = push.read();
                System.out.print("(回退" +(char) temp + ") ");
           }else{
                System.out.print((char) temp);
           }
       }
    }
}

2. 输出字节流OutputStream

定义和结构说明:

IO 中输出字节流的继承图可见上图,可以看出:

OutputStream 是所有的输出字节流的父类,它是一个抽象类。

ByteArrayOutputStream、FileOutputStream是两种基本的介质流,它们分别向Byte 数组、和本地文件中写入数据。PipedOutputStream 是向与其它线程共用的管道中写入数据,

ObjectOutputStream 和所有FilterOutputStream的子类都是装饰流。具体例子跟InputStream是对应的。

实例操作演示:

【案例】向文件中写入字符串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
 * 字节流
 * 向文件中写入字符串
 * */
import java.io.*;
class hello{
   public static void main(String[] args) throws IOException {
       String fileName="D:"+File.separator+"hello.txt";
       File f=new File(fileName);
       OutputStream out =new FileOutputStream(f);
       String str="Hello World";
       byte[] b=str.getBytes();
       out.write(b);
       out.close();
    }
}

你也可以一个字节一个字节的写入文件:

【案例】逐字节写入文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
 * 字节流
 * 向文件中一个字节一个字节的写入字符串
 * */
import java.io.*;
class hello{
   public static void main(String[] args) throws IOException {
       String fileName="D:"+File.separator+"hello.txt";
       File f=new File(fileName);
       OutputStream out =new FileOutputStream(f);
       String str="Hello World!!";
       byte[] b=str.getBytes();
       for (int i = 0; i < b.length; i++) {
           out.write(b[i]);
       }
       out.close();
    }
}

【案例】向文件中追加新内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
 * 字节流
 * 向文件中追加新内容:
 * */
import java.io.*;
class hello{
   public static void main(String[] args) throws IOException {
       String fileName="D:"+File.separator+"hello.txt";
       File f=new File(fileName);
       OutputStream out =new FileOutputStream(f,true);//true表示追加模式,否则为覆盖
       String str="Rollen";
       //String str="\r\nRollen"; 可以换行
       byte[] b=str.getBytes();
       for (int i = 0; i < b.length; i++) {
           out.write(b[i]);
       }
       out.close();
    }
}

【案例】复制文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
 * 文件的复制
 * */
import java.io.*;
class hello{
   public static void main(String[] args) throws IOException {
       if(args.length!=2){
           System.out.println("命令行参数输入有误,请检查");
           System.exit(1);
       }
       File file1=new File(args[0]);
       File file2=new File(args[1]);
         
       if(!file1.exists()){
           System.out.println("被复制的文件不存在");
           System.exit(1);
       }
       InputStream input=new FileInputStream(file1);
       OutputStream output=new FileOutputStream(file2);
       if((input!=null)&&(output!=null)){
           int temp=0;
           while((temp=input.read())!=(-1)){
                output.write(temp);
           }
       }
       input.close();
       output.close();
    }
}

【案例】使用内存操作流将一个大写字母转化为小写字母

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
 * 使用内存操作流将一个大写字母转化为小写字母
 * */
import java.io.*;
class hello{
   public static void main(String[] args) throws IOException {
       String str="ROLLENHOLT";
       ByteArrayInputStream input=new ByteArrayInputStream(str.getBytes());
       ByteArrayOutputStream output=new ByteArrayOutputStream();
       int temp=0;
       while((temp=input.read())!=-1){
           char ch=(char)temp;
           output.write(Character.toLowerCase(ch));
       }
       String outStr=output.toString();
       input.close();
       output.close();
       System.out.println(outStr);
    }
}

【案例】验证管道流:进程间通信

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
 * 验证管道流
 * */
import java.io.*;
  
/**
 * 消息发送类
 * */
class Send implements Runnable{
   private PipedOutputStream out=null;
   public Send() {
       out=new PipedOutputStream();
    }
   public PipedOutputStream getOut(){
       return this.out;
    }
   public void run(){
       String message="hello , Rollen";
       try{
           out.write(message.getBytes());
       }catch (Exception e) {
           e.printStackTrace();
       }try{
           out.close();
       }catch (Exception e) {
           e.printStackTrace();
       }
    }
}
  
/**
 * 接受消息类
 * */
class Recive implements Runnable{
   private PipedInputStream input=null;
   public Recive(){
       this.input=new PipedInputStream();
    }
   public PipedInputStream getInput(){
       return this.input;
    }
   public void run(){
       byte[] b=new byte[1000];
       int len=0;
       try{
           len=this.input.read(b);
       }catch (Exception e) {
           e.printStackTrace();
       }try{
           input.close();
       }catch (Exception e) {
           e.printStackTrace();
       }
       System.out.println("接受的内容为 "+(new String(b,0,len)));
    }
}
/**
 * 测试类
 * */
class hello{
   public static void main(String[] args) throws IOException {
       Send send=new Send();
       Recive recive=new Recive();
        try{
//管道连接
           send.getOut().connect(recive.getInput());
       }catch (Exception e) {
           e.printStackTrace();
       }
       new Thread(send).start();
       new Thread(recive).start();
    }
}

【案例】DataOutputStream类示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class DataOutputStreamDemo{
   public static void main(String[] args) throws IOException{
       File file = new File("d:" + File.separator +"hello.txt");
       char[] ch = { 'A', 'B', 'C' };
       DataOutputStream out = null;
       out = new DataOutputStream(new FileOutputStream(file));
       for(char temp : ch){
           out.writeChar(temp);
       }
       out.close();
    }
}

【案例】ZipOutputStream类

先看一下ZipOutputStream类的继承关系

java.lang.Object

java.io.OutputStream

java.io.FilterOutputStream

java.util.zip.DeflaterOutputStream

java.util.zip.ZipOutputStream

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
  
public class ZipOutputStreamDemo1{
   public static void main(String[] args) throws IOException{
       File file = new File("d:" + File.separator +"hello.txt");
       File zipFile = new File("d:" + File.separator +"hello.zip");
       InputStream input = new FileInputStream(file);
       ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(
                zipFile));
       zipOut.putNextEntry(new ZipEntry(file.getName()));
       // 设置注释
       zipOut.setComment("hello");
       int temp = 0;
       while((temp = input.read()) != -1){
           zipOut.write(temp);
       }
       input.close();
       zipOut.close();
    }
}

【案例】ZipOutputStream类压缩多个文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
  
/**
 * 一次性压缩多个文件
 * */
public class ZipOutputStreamDemo2{
   public static void main(String[] args) throws IOException{
       // 要被压缩的文件夹
       File file = new File("d:" + File.separator +"temp");
       File zipFile = new File("d:" + File.separator + "zipFile.zip");
       InputStream input = null;
       ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(
                zipFile));
       zipOut.setComment("hello");
       if(file.isDirectory()){
           File[] files = file.listFiles();
           for(int i = 0; i < files.length; ++i){
                input = newFileInputStream(files[i]);
                zipOut.putNextEntry(newZipEntry(file.getName()
                        + File.separator +files[i].getName()));
               int temp = 0;
                while((temp = input.read()) !=-1){
                    zipOut.write(temp);
                }
                input.close();
           }
       }
       zipOut.close();
    }
}

【案例】ZipFile类展示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.io.File;
import java.io.IOException;
import java.util.zip.ZipFile;
  
/**
 *ZipFile演示
 * */
public class ZipFileDemo{
   public static void main(String[] args) throws IOException{
       File file = new File("d:" + File.separator +"hello.zip");
       ZipFile zipFile = new ZipFile(file);
       System.out.println("压缩文件的名称为:" + zipFile.getName());
    }
}

【案例】解压缩文件(压缩文件中只有一个文件的情况)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
  
/**
 * 解压缩文件(压缩文件中只有一个文件的情况)
 * */
public class ZipFileDemo2{
   public static void main(String[] args) throws IOException{
       File file = new File("d:" + File.separator +"hello.zip");
       File outFile = new File("d:" + File.separator +"unZipFile.txt");
       ZipFile zipFile = new ZipFile(file);
       ZipEntry entry =zipFile.getEntry("hello.txt");
       InputStream input = zipFile.getInputStream(entry);
       OutputStream output = new FileOutputStream(outFile);
       int temp = 0;
       while((temp = input.read()) != -1){
           output.write(temp);
       }
       input.close();
       output.close();
    }
}

【案例】ZipInputStream类解压缩一个压缩文件中包含多个文件的情况

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
  
/**
 * 解压缩一个压缩文件中包含多个文件的情况
 * */
public class ZipFileDemo3{
   public static void main(String[] args) throws IOException{
        File file = new File("d:" +File.separator + "zipFile.zip");
       File outFile = null;
       ZipFile zipFile = new ZipFile(file);
       ZipInputStream zipInput = new ZipInputStream(new FileInputStream(file));
       ZipEntry entry = null;
        InputStream input = null;
       OutputStream output = null;
       while((entry = zipInput.getNextEntry()) != null){
           System.out.println("解压缩" + entry.getName() + "文件");
           outFile = new File("d:" + File.separator + entry.getName());
           if(!outFile.getParentFile().exists()){
               outFile.getParentFile().mkdir();
           }
           if(!outFile.exists()){
                outFile.createNewFile();
           }
           input = zipFile.getInputStream(entry);
           output = new FileOutputStream(outFile);
           int temp = 0;
           while((temp = input.read()) != -1){
                output.write(temp);
           }
           input.close();
           output.close();
       }
    }
}

3.字节流的输入与输出的对应图示

加载中...

图中蓝色的为主要的对应部分,红色的部分就是不对应部分。紫色的虚线部分代表这些流一般要搭配使用。从上面的图中可以看出Java IO 中的字节流是极其对称的。哲学上讲“存在及合理”,现在我们看看这些字节流中不太对称的几个类吧!

4.几个特殊的输入流类分析

LineNumberInputStream

主要完成从流中读取数据时,会得到相应的行号,至于什么时候分行、在哪里分行是由改类主动确定的,并不是在原始中有这样一个行号。在输出部分没有对应的部分,我们完全可以自己建立一个LineNumberOutputStream,在最初写入时会有一个基准的行号,以后每次遇到换行时会在下一行添加一个行号,看起来也是可以的。好像更不入流了。

PushbackInputStream

其功能是查看最后一个字节,不满意就放入缓冲区。主要用在编译器的语法、词法分析部分。输出部分的BufferedOutputStream 几乎实现相近的功能。

StringBufferInputStream

已经被Deprecated,本身就不应该出现在InputStream部分,主要因为String 应该属于字符流的范围。已经被废弃了,当然输出部分也没有必要需要它了!还允许它存在只是为了保持版本的向下兼容而已。

SequenceInputStream

可以认为是一个工具类,将两个或者多个输入流当成一个输入流依次读取。完全可以从IO 包中去除,还完全不影响IO 包的结构,却让其更“纯洁”――纯洁的Decorator 模式。

【案例】将两个文本文件合并为另外一个文本文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.SequenceInputStream;
  
/**
 * 将两个文本文件合并为另外一个文本文件
 * */
public class SequenceInputStreamDemo{
    public static voidmain(String[] args) throws IOException{
        File file1 = newFile("d:" + File.separator + "hello1.txt");
        File file2 = newFile("d:" + File.separator + "hello2.txt");
        File file3 = newFile("d:" + File.separator + "hello.txt");
        InputStream input1 =new FileInputStream(file1);
        InputStream input2 =new FileInputStream(file2);
        OutputStream output =new FileOutputStream(file3);
        // 合并流
        SequenceInputStreamsis = new SequenceInputStream(input1, input2);
        int temp = 0;
        while((temp =sis.read()) != -1){
           output.write(temp);
        }
        input1.close();
        input2.close();
        output.close();
        sis.close();
    }
}

PrintStream

也可以认为是一个辅助工具。主要可以向其他输出流,或者FileInputStream 写入数据,本身内部实现还是带缓冲的。本质上是对其它流的综合运用的一个工具而已。一样可以踢出IO 包!System.err和System.out 就是PrintStream 的实例!

【案例】使用PrintStream进行输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
 * 使用PrintStream进行输出
 * */
import java.io.*;
  
class hello {
   public static void main(String[] args) throws IOException {
       PrintStream print = new PrintStream(new FileOutputStream(newFile("d:"
                + File.separator +"hello.txt")));
       print.println(true);
       print.println("Rollen");
       print.close();
    }
}

【案例】使用PrintStream进行格式化输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
 * 使用PrintStream进行输出
 * 并进行格式化
 * */
import java.io.*;
class hello {
   public static void main(String[] args) throws IOException {
       PrintStream print = new PrintStream(new FileOutputStream(newFile("d:"
                + File.separator +"hello.txt")));
       String name="Rollen";
       int age=20;
       print.printf("姓名:%s. 年龄:%d.",name,age);
       print.close();
    }
}

【案例】使用OutputStream向屏幕上输出内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
 * 使用OutputStream向屏幕上输出内容
 * */
import java.io.*;
class hello {
   public static void main(String[] args) throws IOException {
       OutputStream out=System.out;
       try{
           out.write("hello".getBytes());
       }catch (Exception e) {
           e.printStackTrace();
       }
       try{
           out.close();
       }catch (Exception e) {
           e.printStackTrace();
       }
    }
}

【案例】输入输出重定向

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
  
/**
 * 为System.out.println()重定向输出
 * */
public class systemDemo{
   public static void main(String[] args){
       // 此刻直接输出到屏幕
       System.out.println("hello");
       File file = new File("d:" + File.separator +"hello.txt");
       try{
           System.setOut(new PrintStream(new FileOutputStream(file)));
       }catch(FileNotFoundException e){
           e.printStackTrace();
       }
       System.out.println("这些内容在文件中才能看到哦!");
    }
}

【案例】使用System.err重定向

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
  
/**
 *System.err重定向这个例子也提示我们可以使用这种方法保存错误信息
 * */
public class systemErr{
   public static void main(String[] args){
       File file = new File("d:" + File.separator +"hello.txt");
       System.err.println("这些在控制台输出");
       try{
           System.setErr(new PrintStream(new FileOutputStream(file)));
       }catch(FileNotFoundException e){
           e.printStackTrace();
       }
       System.err.println("这些在文件中才能看到哦!");
    }
}

【案例】System.in重定向

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
 *System.in重定向
 * */
public class systemIn{
   public static void main(String[] args){
       File file = new File("d:" + File.separator +"hello.txt");
       if(!file.exists()){
           return;
       }else{
           try{
                System.setIn(newFileInputStream(file));
           }catch(FileNotFoundException e){
                e.printStackTrace();
           }
           byte[] bytes = new byte[1024];
           int len = 0;
           try{
                len = System.in.read(bytes);
           }catch(IOException e){
                e.printStackTrace();
           }
           System.out.println("读入的内容为:" + new String(bytes, 0, len));
       }
    }
}

5.字符输入流Reader

定义和说明:

在上面的继承关系图中可以看出:

Reader 是所有的输入字符流的父类,它是一个抽象类。

CharReader、StringReader是两种基本的介质流,它们分别将Char 数组、String中读取数据。PipedReader 是从与其它线程共用的管道中读取数据。

BufferedReader 很明显就是一个装饰器,它和其子类负责装饰其它Reader 对象。

FilterReader 是所有自定义具体装饰流的父类,其子类PushbackReader 对Reader 对象进行装饰,会增加一个行号。

InputStreamReader 是一个连接字节流和字符流的桥梁,它将字节流转变为字符流。FileReader可以说是一个达到此功能、常用的工具类,在其源代码中明显使用了将FileInputStream 转变为Reader 的方法。我们可以从这个类中得到一定的技巧。Reader 中各个类的用途和使用方法基本和InputStream 中的类使用一致。后面会有Reader 与InputStream 的对应关系。

实例操作演示:

【案例】从文件中读取内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
 * 字符流
 * 从文件中读出内容
 * */
import java.io.*;
class hello{
   public static void main(String[] args) throws IOException {
       String fileName="D:"+File.separator+"hello.txt";
       File f=new File(fileName);
       char[] ch=new char[100];
       Reader read=new FileReader(f);
       int count=read.read(ch);
       read.close();
       System.out.println("读入的长度为:"+count);
       System.out.println("内容为"+new String(ch,0,count));
    }
}

注意:当然最好采用循环读取的方式,因为我们有时候不知道文件到底有多大。

【案例】以循环方式从文件中读取内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
 * 字符流
 * 从文件中读出内容
 * */
import java.io.*;
class hello{
   public static void main(String[] args) throws IOException {
       String fileName="D:"+File.separator+"hello.txt";
       File f=new File(fileName);
       char[] ch=new char[100];
       Reader read=new FileReader(f);
       int temp=0;
       int count=0;
       while((temp=read.read())!=(-1)){
           ch[count++]=(char)temp;
       }
       read.close();
       System.out.println("内容为"+new String(ch,0,count));
    }
}

【案例】BufferedReader的小例子

注意:BufferedReader只能接受字符流的缓冲区,因为每一个中文需要占据两个字节,所以需要将System.in这个字节输入流变为字符输入流,采用:

BufferedReader buf = new BufferedReader(newInputStreamReader(System.in));

下面是一个实例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
  
/**
 * 使用缓冲区从键盘上读入内容
 * */
public class BufferedReaderDemo{
   public static void main(String[] args){
       BufferedReader buf = new BufferedReader(
                newInputStreamReader(System.in));
       String str = null;
       System.out.println("请输入内容");
       try{
           str = buf.readLine();
       }catch(IOException e){
           e.printStackTrace();
       }
       System.out.println("你输入的内容是:" + str);
    }
}

【案例】Scanner类实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.util.Scanner;
/**
 *Scanner的小例子,从键盘读数据
 * */
public class ScannerDemo{
    publicstatic void main(String[] args){
       Scanner sca = new Scanner(System.in);
       // 读一个整数
       int temp = sca.nextInt();
       System.out.println(temp);
       //读取浮点数
       float flo=sca.nextFloat();
       System.out.println(flo);
        //读取字符
       //...等等的,都是一些太基础的,就不师范了。
    }
}

【案例】Scanner类从文件中读出内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
  
/**
 *Scanner的小例子,从文件中读内容
 * */
public class ScannerDemo{
   public static void main(String[] args){
  
       File file = new File("d:" + File.separator +"hello.txt");
       Scanner sca = null;
       try{
           sca = new Scanner(file);
       }catch(FileNotFoundException e){
           e.printStackTrace();
       }
       String str = sca.next();
       System.out.println("从文件中读取的内容是:" + str);
    }
}

6.字符输出流Writer

定义和说明:

在上面的关系图中可以看出:

Writer 是所有的输出字符流的父类,它是一个抽象类。

CharArrayWriter、StringWriter 是两种基本的介质流,它们分别向Char 数组、String 中写入数据。

PipedWriter 是向与其它线程共用的管道中写入数据,

BufferedWriter 是一个装饰器为Writer 提供缓冲功能。

PrintWriter 和PrintStream 极其类似,功能和使用也非常相似。

OutputStreamWriter 是OutputStream 到Writer 转换的桥梁,它的子类FileWriter 其实就是一个实现此功能的具体类(具体可以研究一SourceCode)。功能和使用和OutputStream 极其类似,后面会有它们的对应图。

实例操作演示:

【案例】向文件中写入数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
 * 字符流
 * 写入数据
 * */
import java.io.*;
class hello{
   public static void main(String[] args) throws IOException {
       String fileName="D:"+File.separator+"hello.txt";
       File f=new File(fileName);
       Writer out =new FileWriter(f);
       String str="hello";
       out.write(str);
       out.close();
    }
}

注意:这个例子上之前的例子没什么区别,只是你可以直接输入字符串,而不需要你将字符串转化为字节数组。当你如果想问文件中追加内容的时候,可以使用将上面的声明out的哪一行换为:

Writer out =new FileWriter(f,true);

这样,当你运行程序的时候,会发现文件内容变为:hellohello如果想在文件中换行的话,需要使用“\r\n”比如将str变为String str="\r\nhello";这样文件追加的str的内容就会换行了。

7.字符流的输入与输出的对应

加载中...

8.字符流与字节流转换

转换流的特点:

(1)其是字符流和字节流之间的桥梁

(2)可对读取到的字节数据经过指定编码转换成字符

(3)可对读取到的字符数据经过指定编码转换成字节

何时使用转换流?

当字节和字符之间有转换动作时;

流操作的数据需要编码或解码时。

具体的对象体现:

InputStreamReader:字节到字符的桥梁

OutputStreamWriter:字符到字节的桥梁

这两个流对象是字符体系中的成员,它们有转换作用,本身又是字符流,所以在构造的时候需要传入字节流对象进来。

字节流和字符流转换实例:

【案例】将字节输出流转化为字符输出流

1
2
3
4
5
6
7
8
9
10
11
12
13
/**
 * 将字节输出流转化为字符输出流
 * */
import java.io.*;
class hello{
   public static void main(String[] args) throws IOException {
       String fileName= "d:"+File.separator+"hello.txt";
       File file=new File(fileName);
       Writer out=new OutputStreamWriter(new FileOutputStream(file));
       out.write("hello");
       out.close();
    }
}

【案例】将字节输入流转换为字符输入流

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
 * 将字节输入流变为字符输入流
 * */
import java.io.*;
class hello{
   public static void main(String[] args) throws IOException {
       String fileName= "d:"+File.separator+"hello.txt";
       File file=new File(fileName);
       Reader read=new InputStreamReader(new FileInputStream(file));
       char[] b=new char[100];
       int len=read.read(b);
       System.out.println(new String(b,0,len));
       read.close();
    }
}

9.File类

File类是对文件系统中文件以及文件夹进行封装的对象,可以通过对象的思想来操作文件和文件夹。 File类保存文件或目录的各种元数据信息,包括文件名、文件长度、最后修改时间、是否可读、获取当前文件的路径名,判断指定文件是否存在、获得当前目录中的文件列表,创建、删除文件和目录等方法。

【案例 】创建一个文件

1
2
3
4
5
6
7
8
9
10
11
import java.io.*;
class hello{
   public static void main(String[] args) {
       File f=new File("D:\\hello.txt");
       try{
           f.createNewFile();
       }catch (Exception e) {
           e.printStackTrace();
       }
    }
}

【案例2】File类的两个常量

1
2
3
4
5
6
7
import java.io.*;
class hello{
   public static void main(String[] args) {
       System.out.println(File.separator);
       System.out.println(File.pathSeparator);
    }
}

此处多说几句:有些同学可能认为,我直接在windows下使用\进行分割不行吗?当然是可以的。但是在linux下就不是\了。所以,要想使得我们的代码跨平台,更加健壮,所以,大家都采用这两个常量吧,其实也多写不了几行。

【案例3】File类中的常量改写案例1的代码:

1
2
3
4
5
6
7
8
9
10
11
12
import java.io.*;
class hello{
   public static void main(String[] args) {
       String fileName="D:"+File.separator+"hello.txt";
       File f=new File(fileName);
       try{
           f.createNewFile();
       }catch (Exception e) {
           e.printStackTrace();
       }
    }
}

【案例4】删除一个文件(或者文件夹)

1
2
3
4
5
6
7
8
9
10
11
12
13
import java.io.*;
class hello{
   public static void main(String[] args) {
       String fileName="D:"+File.separator+"hello.txt";
       File f=new File(fileName);
       if(f.exists()){
           f.delete();
       }else{
           System.out.println("文件不存在");
       }
         
    }
}

【案例5】创建一个文件夹

1
2
3
4
5
6
7
8
9
10
11
/**
 * 创建一个文件夹
 * */
import java.io.*;
class hello{
   public static void main(String[] args) {
       String fileName="D:"+File.separator+"hello";
       File f=new File(fileName);
       f.mkdir();
    }
}

【案例6】列出目录下的所有文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
 * 使用list列出指定目录的全部文件
 * */
import java.io.*;
class hello{
   public static void main(String[] args) {
       String fileName="D:"+File.separator;
       File f=new File(fileName);
       String[] str=f.list();
       for (int i = 0; i < str.length; i++) {
           System.out.println(str[i]);
       }
    }
}

注意使用list返回的是String数组,。而且列出的不是完整路径,如果想列出完整路径的话,需要使用listFiles.它返回的是File的数组。

【案例7】列出指定目录的全部文件(包括隐藏文件):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
 * 使用listFiles列出指定目录的全部文件
 * listFiles输出的是完整路径
 * */
import java.io.*;
class hello{
   public static void main(String[] args) {
       String fileName="D:"+File.separator;
       File f=new File(fileName);
       File[] str=f.listFiles();
       for (int i = 0; i < str.length; i++) {
           System.out.println(str[i]);
       }
    }
}

【案例8】判断一个指定的路径是否为目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
 * 使用isDirectory判断一个指定的路径是否为目录
 * */
import java.io.*;
class hello{
   public static void main(String[] args) {
       String fileName="D:"+File.separator;
       File f=new File(fileName);
       if(f.isDirectory()){
           System.out.println("YES");
       }else{
           System.out.println("NO");
       }
    }
}

【案例9】递归搜索指定目录的全部内容,包括文件和文件夹

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
* 列出指定目录的全部内容
 * */
import java.io.*;
class hello{
   public static void main(String[] args) {
       String fileName="D:"+File.separator;
       File f=new File(fileName);
       print(f);
    }
   public static void print(File f){
       if(f!=null){
           if(f.isDirectory()){
                File[] fileArray=f.listFiles();
                if(fileArray!=null){
                    for (int i = 0; i

10.RandomAccessFile类

该对象并不是流体系中的一员,其封装了字节流,同时还封装了一个缓冲区(字符数组),通过内部的指针来操作字符数组中的数据。该对象特点:

该对象只能操作文件,所以构造函数接收两种类型的参数:a.字符串文件路径;b.File对象。

该对象既可以对文件进行读操作,也能进行写操作,在进行对象实例化时可指定操作模式(r,rw)

注意:该对象在实例化时,如果要操作的文件不存在,会自动创建;如果文件存在,写数据未指定位置,会从头开始写,即覆盖原有的内容。可以用于多线程下载或多个线程同时写数据到文件。

【案例】使用RandomAccessFile写入文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
 * 使用RandomAccessFile写入文件
 * */
import java.io.*;
class hello{
    public static void main(String[]args) throws IOException {
        StringfileName="D:"+File.separator+"hello.txt";
        File f=new File(fileName);
        RandomAccessFile demo=newRandomAccessFile(f,"rw");
       demo.writeBytes("asdsad");
        demo.writeInt(12);
        demo.writeBoolean(true);
        demo.writeChar('A');
        demo.writeFloat(1.21f);
        demo.writeDouble(12.123);
        demo.close(); 
    }
}

Java IO流的高级概念

编码问题

【案例 】取得本地的默认编码

1
2
3
4
5
6
7
8
/**
 * 取得本地的默认编码
 * */
publicclass CharSetDemo{
    public static void main(String[] args){
        System.out.println("系统默认编码为:"+ System.getProperty("file.encoding"));
    }
}

【案例 】乱码的产生

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
  
/**
 * 乱码的产生
 * */
public class CharSetDemo2{
    public static void main(String[] args) throws IOException{
        File file = new File("d:" + File.separator + "hello.txt");
        OutputStream out = new FileOutputStream(file);
        byte[] bytes = "你好".getBytes("ISO8859-1");
        out.write(bytes);
        out.close();
    }//输出结果为乱码,系统默认编码为GBK,而此处编码为ISO8859-1
}

对象的序列化

对象序列化就是把一个对象变为二进制数据流的一种方法。

一个类要想被序列化,就行必须实现java.io.Serializable接口。虽然这个接口中没有任何方法,就如同之前的cloneable接口一样。实现了这个接口之后,就表示这个类具有被序列化的能力。先让我们实现一个具有序列化能力的类吧:

【案例 】实现具有序列化能力的类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import java.io.*;
/**
 * 实现具有序列化能力的类
 * */
public class SerializableDemo implements Serializable{
    public SerializableDemo(){
         
    }
    publicSerializableDemo(String name, int age){
        this.name=name;
        this.age=age;
    }
    @Override
    public String toString(){
        return "姓名:"+name+"  年龄:"+age;
    }
    private String name;
    private int age;
}

【案例 】序列化一个对象 – ObjectOutputStream

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.io.Serializable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
/**
 * 实现具有序列化能力的类
 * */
public class Person implements Serializable{
    public Person(){
     }
    public Person(String name,int age){
        this.name = name;
        this.age = age;
    }
    @Override
    public String toString(){
        return "姓名:" +name + "  年龄:" +age;
    }
    private String name;
    private int age;
}
/**
 * 示范ObjectOutputStream
 * */
public class ObjectOutputStreamDemo{
    public static voidmain(String[] args) throws IOException{
        File file = newFile("d:" + File.separator + "hello.txt");
        ObjectOutputStream oos= new ObjectOutputStream(new FileOutputStream(
                file));
        oos.writeObject(newPerson("rollen", 20));
        oos.close();
    }
}

【案例 】反序列化—ObjectInputStream

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
  
/**
 * ObjectInputStream示范
 * */
public class ObjectInputStreamDemo{
    public static voidmain(String[] args) throws Exception{
        File file = new File("d:" +File.separator + "hello.txt");
        ObjectInputStreaminput = new ObjectInputStream(new FileInputStream(
                file));
        Object obj =input.readObject();
        input.close();
        System.out.println(obj);
    }
}

注意:被Serializable接口声明的类的对象的属性都将被序列化,但是如果想自定义序列化的内容的时候,就需要实现Externalizable接口。

当一个类要使用Externalizable这个接口的时候,这个类中必须要有一个无参的构造函数,如果没有的话,在构造的时候会产生异常,这是因为在反序列话的时候会默认调用无参的构造函数。

现在我们来演示一下序列化和反序列话:

【案例 】使用Externalizable来定制序列化和反序列化操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package IO;
  
import java.io.Externalizable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
  
/**
 * 序列化和反序列化的操作
 * */
public class ExternalizableDemo{
    public static voidmain(String[] args) throws Exception{
        ser(); // 序列化
        dser(); // 反序列话
    }
  
    public static void ser()throws Exception{
        File file = newFile("d:" + File.separator + "hello.txt");
        ObjectOutputStream out= new ObjectOutputStream(new FileOutputStream(
                file));
        out.writeObject(newPerson("rollen", 20));
        out.close();
    }
  
    public static void dser()throws Exception{
        File file = newFile("d:" + File.separator + "hello.txt");
        ObjectInputStreaminput = new ObjectInputStream(new FileInputStream(
                file));
        Object obj =input.readObject();
        input.close();
       System.out.println(obj);
    }
}
  
class Person implements Externalizable{
    public Person(){
  
    }
  
    public Person(String name,int age){
        this.name = name;
        this.age = age;
    }
  
    @Override
    public String toString(){
        return "姓名:" +name + "  年龄:" +age;
    }
  
    // 复写这个方法,根据需要可以保存的属性或者具体内容,在序列化的时候使用
    @Override
    public voidwriteExternal(ObjectOutput out) throws IOException{
       out.writeObject(this.name);
        out.writeInt(age);
    }
  
    // 复写这个方法,根据需要读取内容 反序列话的时候需要
    @Override
    public voidreadExternal(ObjectInput in) throws IOException,
           ClassNotFoundException{
        this.name = (String)in.readObject();
        this.age =in.readInt();
    }
  
    private String name;
    private int age;
}

注意:Serializable接口实现的操作其实是吧一个对象中的全部属性进行序列化,当然也可以使用我们上使用是Externalizable接口以实现部分属性的序列化,但是这样的操作比较麻烦,

当我们使用Serializable接口实现序列化操作的时候,如果一个对象的某一个属性不想被序列化保存下来,那么我们可以使用transient关键字进行说明:

【案例 】使用transient关键字定制序列化和反序列化操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package IO;
  
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
  
/**
 * 序列化和反序列化的操作
 * */
public class serDemo{
    public static voidmain(String[] args) throws Exception{
        ser(); // 序列化
        dser(); // 反序列话
    }
  
    public static void ser()throws Exception{
        File file = newFile("d:" + File.separator + "hello.txt");
        ObjectOutputStream out= new ObjectOutputStream(new FileOutputStream(
                file));
        out.writeObject(newPerson1("rollen", 20));
        out.close();
    }
  
    public static void dser()throws Exception{
        File file = newFile("d:" + File.separator + "hello.txt");
        ObjectInputStreaminput = new ObjectInputStream(new FileInputStream(
                file));
        Object obj =input.readObject();
        input.close();
       System.out.println(obj);
    }
}
  
class Person1 implements Serializable{
    public Person1(){
  
    }
  
    public Person1(Stringname, int age){
        this.name = name;
        this.age = age;
    }
  
    @Override
    public String toString(){
        return "姓名:" +name + "  年龄:" +age;
    }
  
    // 注意这里
    private transient Stringname;
    private int age;
}

【运行结果】:

姓名:null 年龄:20


Java读写IO流详解 一、IO流要明确的几点 (1)明确要操作的数据是数据源还是数据目的(是读还是写) 源/读 InputStream Reader 目的/写 OutputStream Writer (2)明确操作设备上的数据是字节还是文本 源/读 字节:InputStream 文本:Reader 目的/写 字节:OutputStream ... 阅读详情

相关推荐

INCA刷写App文件必看:A2l文件配置与ProF文件安装详解(2023最新版)

本文深入解析了使用INCA进行ECU应用程序刷写时,A2l文件配置与ProF文件安装的关键细节与避坑指南。重点阐述了A2l文件必须与待刷写二进制文件严格匹配的核心逻辑,以及ProF文件与Bootloader版本强相关的安装策略,帮助开发者解决刷写过程中的常见失败问题,确保刷写流程的稳定与可靠。

milk8的博客 591

java IO流案例详解

流的概念和作用 学习Java IO,不得不提到的就是JavaIO流。 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象。即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输特性将流抽象为各种类,方便更直观的进行数据操作。 IO流的分类 根据处理数据类型的不同分为:字符流和字节流 根据数据流向不同分为:输入流和输出流 字符流和字节流

Code-lover's Learning Notes 3759

运放 + MOS管构成的恒流电路分析及实用环境器件参数选择

恒流电路

五花带点肉 2万+

Java IO流详解(史上最全18个案例代码)

Java IO流是Java中用于输入和输出数据的机制,它提供了一种灵活且强大的方式来处理文件、网络连接和其他数据源。IO流主要分为字节流和字符流两种类型,其中字节流适用于处理二进制数据,而字符流则适用于处理文本数据。Java IO流的核心是输入流和输出流,输入流用于从数据源读取数据,输出流用于向数据源写入数据。

Liugx的博客 5137

2024年全国大学生信息安全竞赛安徽省赛网络系统建设与运维赛项-网络构建真题

该网络工程方案主要包含基础配置、有线网络、无线网络、出口网络、运维配置和SDN网络六个部分。基础配置包括设备接口设置、SSH服务开启和SNMP功能配置。有线网络部分重点部署VLAN、MSTP、VRRP和OSPF协议,实现网络冗余和负载均衡。无线网络规划了AP点位、信号覆盖和用户接入策略,包括SSID配置、频段优化和安全认证。出口网络配置了NAT转换、VPN隧道和流量审计功能。运维方面搭建了监控平台实时监测设备状态。SDN网络部分通过控制器实现流量管控。整个方案实现了多业务隔离、高可用性网络架构和

weixin_42221934的博客 1174

IMU标定中的Allen方差

Allen方差 Allen方差是IMU标定任务中的常用工具。IMU标定可以分为确定性误差和随机误差,确定性误差包括:scale、bias、misalignment。随机误差则主要是高斯白噪声和bias随机游走。Allen方差主要用于标定随机误差。 Allen方差获取 Allen方差的计算方法如下: 保持IMU静止, 采集N个数据点。 将N个点定长分组,常用的交叠式Allen方差如下(n=3): 对于每个子集,计算均值后,计算相邻两组的方差,得到的时间-方差曲线即为Allen方差。 Alle

realjc的博客 5734

Java8 Stream深度解析:30个案例3万字助你精通集合筛选、归约、分组与聚合操作

Java 8引入了Stream API,这是一个用于处理集合元素的强大工具。通过Stream流,你可以以一种声明式的方式处理数据,使得代码更加简洁、易读且易于维护。在本文中,我们将深入探讨Java8 Stream流的使用,包括其基本概念、常用操作以及实际应用示例。

码到三十五 1689

Unlocking Decoding-time Controllability: Gradient-FreeMulti-Objective Alignment with Contrastive Pr

将基于庞大网络语料库训练的大型语言模型(LLMs)(OpenAI,2023;Touvron 等,2023;Google,2023)与人类偏好对齐,是减少产生不安全(Wei 等,2023)、幻觉(Zhang 等,2023b)和有偏见(Gallegos 等,2023)内容的重要一步。随着偏好学习技术(如 PPO(Schulman 等,2017)、DPO(Rafailov 等,2023)及其他变体(Azar 等,2023;Ethayarajh 等,2024;

qq_52053775的博客 1169

9张图,32个案例带你轻松玩转Java stream

Java8 中增加了 Stream 处理,可以配合 Lambda 表达式来使用,让操作集合非常便利。虽然我们平时经常使用 Stream,但用到的方法其实非常少,这篇文章就来完整的介绍 Stream 的使用。 Stream 提供的方法非常多,按照调用当前方法是否结束流处理,可以分为中间操作和结束操作。 对于中间操作,又可以分为有状态的操作和无状态操作: 无状态的操作是指当前元素的操作不受前面元素的影响。 有状态的操作是指当前元素的操作需要等所有元素处理完之后才能进行。 对于结束操作,又可以分为

q66562636的博客 347

嵌入式工程师成长之路(0)----认识元件

在电子设计与维修中,认识元件是成功的关键!面对琳琅满目的电阻、电容、电感与二极管,样子形式千奇百怪,你是否也曾陷入困难? 本文基于行业权威分类标准,结合十年工程师实战经验,深度解析4大类、30+子类电子元件的核心特性、应用场景与避坑指南,助你从“小白”秒变“电子高手”!

云轩阁 11万+

AI编程工具到底是初学者的绊脚石还是垫脚石?

现在是AI时代,我们要做的,是学会拥抱AI、使用AI,成为AI的驾驭者,如果你因为害怕使用AI学不到东西,那么大可不必,因为AI必然是今后的趋势,相信会用AI在今后的求职面试中也会有很大的占比。另外我之前写了多篇有关AI编程开发工具Trae的使用技巧和教程,需要的话你可以关注公众号后通过菜单栏获取。另外我也把所有Trae相关的文章整合到了腾讯ima知识库,感兴趣的可以关注公众号以后回复。

cpp加油站的专栏 188

Java8中stream流处理数据21个小案例(学习使用)

案例19:重量大于20的分为一组,重量小于20的分为一组,只要分组的重量最大的。案例14:按颜色分组,挑选出每个分组中重量最大的,重量一样的话,根据城市名字。案例16:按照颜色分组,找出每个分组下的最大值,总数。案例20:按照重量大于20的一组,小于20的一组,找出每组个数。案例2:筛选要么是重量大于23的红苹果,要么是绿苹果的集合。案例17:重量大于20的分为一组,重量小于20的分为一组。案例15:同时找出苹果的总数量,最大值,最小值,平均值。案例12:按照苹果颜色分组,颜色一样按照重量分组。

七一 839

【DaVinci Developer专题】-28-Client Server Port Interface中useArgumentType、useArrayBaseType和useVoid介绍

共7页 目录 1 ImplPolicy = useArgumentType 2 ImplPolicy = useArrayBaseType 3 ImplPolicy = useVoid 4 三者代码差异部分 5 结尾 <--返回「实战篇」博文-总目录--> <--返回「Autosar从入门到精通-实战篇」专栏主页--> 关键字: 缩略语/术语 含义 DEV DaVinci Developer

「汽车电子助手」的博客 1502

【Java IO流】IO流详解

IO,即InputOutput,也就是输入和输出,指应用程序和外部设备之间的数据传递,常见的外部设备包括磁盘文件、管道、网络连接Java中是通过流处理IO的,那么什么是流?流(Stream),是一个抽象的概念,是指一连串的数据(字符或字节),是以先进先出的方式发送信息的通道当Java程序需要读取数据的时候,就会开启一个通向数据源的流,这个数据源可以是磁盘文件、内存、网络连接等等。类似地,当Java程序需要写入数据的时候,就会开启一个通向目的地的流。

m0_55908255的博客 1695

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

Java IO流详解

原文 Java IO流详解 1.什么是IO Java中I/O操作主要是指使用Java进行输入,输出操作. Java所有的I/O机制都是基于数据流进行输入输出,这些数据流表示了字符或者字节数据的流动序列。Java的I/O流提供了读写数据的标准方法。任何Java中表示数据源的对象都会提供以数据流的方式读写它的数据的方法。 IO又分为流IO(java.io)和块IO(java.nio) Java.io是大多数面向数据流的输入/输出类的主要软件包。此外,Java也对块传输提供支持,在核心库 java.nio中采用的

ss810540895的博客 1万+

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

Java:IO流详解

Java:IO流详解

游手好闲 1万+

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

Java 常用IO流操作详解

IO流大家肯定不陌生,简单整理了一下常用IO流基本用法,其他的IO流以后有时间在整理。 1.基本概念IO:Java对数据的操作是通过流的方式,IO流用来处理设备之间的数据传输,上传文件和下载文件,Java用于操作流的对象都在IO包中。2.IO流的分类图示:(主要IO流) 3.字节流(1).字节流基类1).InputStreamInputStream:字节输入流基类,抽象类是表示字节输入流的所有

MAGIC 3万+

JAVA—IO流详解

IO:Input/Output即输入&输出,也称之为流(河流,水流),指的是数据从一个地点到另一个地点过程;对于计算机来说文件的拷贝过程,文件的编写保存,显示功能都是使用到IO;将数据的传输过程看做一个输入输出。现实角度考虑可以将耳朵作为输入流,将嘴巴当做输出流输入输出介质:文件网络键盘(输入)显示器(输出)

weixin_48006490的博客 8624

【Java】各种IO流知识详解

Java IO流是Java编程中用于处理输入输出操作的核心机制,它提供了一套统一的接口来处理不同类型的数据源和目标。无论是文件操作、网络通信还是内存数据处理,IO流都扮演着关键角色。IO流(Input/Output Stream)是Java中用于数据传输的抽象概念。流代表了一个有序的数据序列,数据可以从一个地方流向另一个地方。IO流的设计模式采用了装饰器模式,通过组合不同的流来实现复杂的功能。Java IO流主要分为两大类:字节流和字符流,每种又分为输入流和输出流。 具体分类 字节流:处理二进制数据,如图

WizLC 971

【12章 Java IO流】程序员必看 IO流 详解

【12章 Java IO流:四大抽象类、输出流、输入流、File类、节点流、缓冲流、转换流、打印流、数据流、对象流、随机存取文件流】

小鳴人的博客 2万+

Java-详解IO流原理及流的分类

Java IO原理 一、概述 (1)I/O是Input/Output的缩写, I/O技术是非常实用的技术,用于处理设备之间的数据传输。如读/写文件,网络通讯等。 输入input:读取外部数据(磁 盘、光盘等存储设备的数据)到程序(内存)中。 输出output:将程序(内存)数据输出到磁盘、光盘等存储设 备中。 (2)Java程序中,对于数据的输入/输出操作以“流(stream)” 的方式进行。 (3) java.io包下提供了各种“流”类和接口,用以获取不同种 这里是引用 类的数据,并通过

weixin_52533007的博客 3188

Java IO 流详解与示例

IO(Input/Output)流是 Java 中用于处理输入和输出操作的机制,它让程序能够读取外部数据(如文件、键盘输入),或将数据输出到外部设备(如文件、屏幕)。

ABCDjseu114514的博客 553
上一篇: Java网络编程详解
下一篇: Java集合类: Set、List、Map、Queue使用场景梳理
DarlingRay
博客等级 码龄8年 7粉丝 2原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值