Java 数据输入输出流 内存操作流 打印流

21.01数据输入输出流的概述和使用

  A:数据输入输出流的概述
  	 数据输入流:DataInputStream
  	 数据输出流:DataOutputStream
  	 特点:可以写基本数据类型,可以读取基本数据类型
public static void main(String[] args) throws IOException {
        //数据输入输出流,此流最大的特点,就是能够读写基本数据类型
        /*  数据输入流:
        DataInputStream
        数据输出流:
        DataOutputStream*/

        //writeData();
        //你怎么写的,你就怎么读取,顺序不要乱
        DataInputStream in = new DataInputStream(new FileInputStream("a.txt"));
        int num = in.readInt();
        System.out.println(num);
        boolean b = in.readBoolean();
        System.out.println(b);
        String s = in.readUTF();
        System.out.println(s);
        double v = in.readDouble();
        System.out.println(v);
    }

    private static void writeData() throws IOException {
        DataOutputStream ds = new DataOutputStream(new FileOutputStream("a.txt"));
        ds.writeInt(500);
        ds.writeBoolean(true);
        ds.writeUTF("hahaha");  //字符串
        ds.writeDouble(3.14);
        ds.close();
    }

21.02_内存操作流的概述和使用

  A:内存操作流的概述
  	 a:操作字节数组
  	 	ByteArrayOutputStream
  	 	ByteArrayInputStream
  	 b:操作字符数组
  	 	CharArrayWrite
  	 	CharArrayReader
  	 c:操作字符串
  	 	StringWriter
  	 	StringReader
public static void main(String[] args) throws IOException {
        // 内存操作流: 不会关联文件,只是在内存中进行数据的读写,内存操作
        //流,会自己在内存中维护了一个缓存,把数据维护在缓存中
     /*   a:
        操作字节数组
        ByteArrayOutputStream
        ByteArrayInputStream
        此流关闭无效,所以无需关闭
        b:
        操作字符数组
        CharArrayWrite
        CharArrayReader
        c:
        操作字符串
        StringWriter
        StringReader*/

        // ByteArrayOutputStream
        //ByteArrayInputStream


      /*  此类实现了一个输出流,其中的数据被写入一个 byte 数组。
        缓冲区会随着数据的不断写入而自动增长。
        可使用 toByteArray () 和 toString () 获取数据。
        关闭 ByteArrayOutputStream 无效。此类中的方法在关闭此流后仍可被
        调用,而不会产生任何 IOException。
      */
     /*   构造方法摘要
        ByteArrayOutputStream()
        创建一个新的 byte 数组输出流。
     */

        //自己在内存中维护了一个字节数组充当缓存,你用他写入的数据,就会放
        //到他维护的这个字节数组中, 缓冲区会随着数据的不断写入而自动增长
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bos.write("aaa".getBytes());
        bos.write("bbb".getBytes());
        bos.write("ccc".getBytes());
        //取出ByteArrayOutputStream 他所维护的那个字节数组
        byte[] bytes = bos.toByteArray();
        String s = new String(bytes);
        System.out.println(s);
        //如果放的是,字符串的字节数据,你可以直接调用toString()
        String s2 = bos.toString();
        System.out.println(s2);
    }
public static void main(String[] args) throws IOException {
        /*  操作字节数组
                ByteArrayOutputStream
                ByteArrayInputStream*/
       /* ByteArrayInputStream( byte[] buf)
        创建一个 ByteArrayInputStream,使用 buf 作为其缓冲区数组。*/
        byte[] bytes = "西部开源科技教育有限公司".getBytes();
        ByteArrayInputStream in = new ByteArrayInputStream(bytes);
        byte[] bytes1 = new byte[1024];
        int len = in.read(bytes1);
        String s = new String(bytes1, 0, len);
        System.out.println(s);
    }
public static void main(String[] args) throws IOException {
        //把多个文件合并成一个文件
        //比如:我要把两首歌 合并成一首歌
        FileInputStream in1 = new FileInputStream("C:\\Users\\57642\\Desktop\\The Weeknd,Daft Punk - Starboy.mp3");
        FileInputStream in2 = new FileInputStream("C:\\Users\\57642\\Desktop\\Kanye West,Daft Punk - Stronger.mp3");
        FileOutputStream allOut = new FileOutputStream("C:\\Users\\57642\\Desktop\\The Weeknd.mp3");
        ArrayList<FileInputStream> list = new ArrayList<>();
        list.add(in1);
        list.add(in2);

        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        byte[] bytes = new byte[1024 * 8];
        int len = 0;
        for (FileInputStream in : list) {
            while((len = in.read(bytes)) != -1){
                bos.write(bytes,0,len);
            }
            in.close();
        }

        //取出两首歌的字节数据
        byte[] allBytes = bos.toByteArray();

        ByteArrayInputStream bin = new ByteArrayInputStream(allBytes);

        byte[] bytes2 = new byte[1024 * 8];
        int len2 = 0;

        while((len2 = bin.read(bytes2)) != -1){
            allOut.write(bytes2,0,len2);
            allOut.flush();
        }
        allOut.close();
        System.out.println("合并完毕");
    }
public static void main(String[] args) throws IOException {
        //把多个文件合并成一个文件
        //比如:我要把两首歌 合并成一首歌
        FileInputStream in1 = new FileInputStream("C:\\Users\\57642\\Desktop\\The Weeknd,Daft Punk - Starboy.mp3");
        FileInputStream in2 = new FileInputStream("C:\\Users\\57642\\Desktop\\Kanye West,Daft Punk - Stronger.mp3");
        FileOutputStream allOut = new FileOutputStream("C:\\Users\\57642\\Desktop\\The Weeknd2.mp3");
        ArrayList<FileInputStream> list = new ArrayList<>();
        list.add(in1);
        list.add(in2);

        byte[] bytes2 = new byte[1024 * 8];
        int len = 0;
        for (FileInputStream in : list) {
            while((len = in.read(bytes2)) != -1){
                allOut.write(bytes2,0,len);
                allOut.flush();
            }
            in.close();
        }
        allOut.close();
    }
public static void main(String[] args) throws IOException {
        /* b:
        操作字符数组
        CharArrayWrite
        CharArrayReader*/
        CharArrayWriter charArrayWriter = new CharArrayWriter();
        charArrayWriter.write("abc");
        charArrayWriter.write("abc");
        charArrayWriter.write("abc");
        charArrayWriter.write("abc");
        String s = charArrayWriter.toString();
        char[] chars = charArrayWriter.toCharArray();
        String s1 = String.valueOf(chars);
        System.out.println(s);
        System.out.println(s1);
    }
public static void main(String[] args) throws IOException {
        /*  StringWriter
            StringReader*/
        StringWriter stringWriter = new StringWriter();
        stringWriter.write("abc");
        stringWriter.write("abc");
        stringWriter.write("abc");
        stringWriter.write("abc");
        stringWriter.write("abc");
        String s = stringWriter.toString();
        System.out.println(s);
    }

21.03_打印流

  A:打印流的特点
  		字节打印流
  		字符打印流
  		a:打印流只能操作目的地,不能操作数据源(不能进行读取数据)
  		b:可以操作任意数据类型的数据 调用print() 方法可以写任意数据类型
  		c:如果我们启用自动刷新,那么在调用println、printf或format方法中
  		   的一个方法的时候,就会完成自动刷新

	/*
		通过以下构造创建对象 能够启动自动刷新 然后调用println、printf或
		format方法中的一个方法的时候,会完成自动刷新

	 *	public PrintWriter(OutputStream out, boolean autoFlush)
	 													启动自动刷新
	 *	public PrintWriter(Writer out, boolean autoFlush)
	 													启动自动刷新

		d:这个流可以直接对文件进行操作(可以直接操作文件的流:就是构造方
		   法的参数可以传递文件或者文件路径)
public static void main(String[] args) throws IOException {
        //打印流:只能写,不是成对的,只有一个
        //字符打印流  PrintWriter
        //字节打印流 PrintStream
        //自己创建出来的打印流,关联的是文件,所以你在调用println("abc");
        //会输出到文件中
        PrintStream printStream = new PrintStream("e.txt");
        printStream.write("你好".getBytes());
        printStream.println("abc");
        printStream.println(100);
        printStream.println(3.14);
        //out “标准”输出流。此流已打开并准备接受输出数据。通常,此流对应
        //于显示器

        //System.out; 获取出的PrintStream 他所关联的设备是屏幕,所以你打
        //印的数据,就会输出到屏幕上。
        PrintStream out = System.out;
        out.println(200);
        out.write("hahah".getBytes());
    }
public static void main(String[] args) throws FileNotFoundException {
        //true表示开启自动刷新,但对write方法无效
        //如果启用了自动刷新,则只有在调用println、printf或format的其中
        //一个方法时才能完成此操作
        PrintWriter pw = new PrintWriter(new FileOutputStream("g.txt"),true);
        pw.write("abc");
        pw.write("abc");
        pw.write("abc");
        pw.write("abc");

        pw.println("abc");
        pw.println("abc");
        pw.println("abc");
        pw.println("abc");
        pw.println("abc");
    }
public static void main(String[] args) throws IOException {
        /*
        案例:
            打印流复制文本文件
     */
        BufferedReader br = new BufferedReader(new FileReader("g.txt"));
        PrintWriter pw = new PrintWriter(new FileOutputStream("name.txt"),true);
        while (true){
            String line = br.readLine();
            if (line == null) {
                break;
            }
            pw.println(line);
        }
        br.close();
        pw.close();
    }

21.04_标准输入输出流概述和输出语句

  A:标准输入输出流
  	 在System这个类中存在两个静态的成员变量
  	 	public static final InputStream in:标准输入流,对应的设备是键盘
  	 	public static final PrintStream out:标准输出流,对应的设备就是
  	 										 显示器
  	 		System.in的类型是InputStream
  	 		System.out的类型是PrintStream是OutputStream的孙子类
  	 						  FilterOutputStream的子类
public static void main(String[] args) throws IOException {
        //“标准”输入流,此流已打开并准备提供输入数据,通常,此流对应于键
        //盘输入
        /*InputStream in = System.in; //BufferedInputStream
        System.out.println(in);

        Scanner scanner = new Scanner(System.in);*/

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        while (true){
            System.out.println("请输入内容");
            String s = br.readLine();
            //自己定义一个结束标记
            if("886".equals(s)){
                break;
            }
            System.out.println(s);
        }
    }
public static void main(String[] args) throws FileNotFoundException {
        /*
            Scanner(File source)
            构造一个新的 Scanner,它生成的值是从指定文件扫描的
         */
        Scanner scanner = new Scanner(new File("demo.java"));
        PrintWriter pw = new PrintWriter("demo2.java");

        while (scanner.hasNextLine()){  //判断有没有下一行
            String s = scanner.nextLine();
            //System.out.println(s);
            pw.println(s);
            pw.flush();
        }
        scanner.close();
        pw.close();
    }

21.05_随机访问流概述

  A:随机访问流概述
  		RandomAccessFile概述 最大特点 能读能写
  		RandomAccessFile类不属于流,是Object类的子类。但它融合了
  		InputStream和OutputStream的功能。
  		支持对随机访问文件的读取和写入

		RandomAccessFile的父类是Object,这个流对象可以用来读取数据也可以
		用来写数据,可以操作任意数据类型的数据。

		我们通过getFilePointer方法获取文件指针,并且通过seek方法设置文件
		指针
public static void main(String[] args) throws IOException {
        //模拟断点复制
        File file = new File("The Weeknd,Daft Punk - Starboy.ncm");
        File file1 = new File("C:\\Users\\57642\\Desktop\\333.ncm");
        RandomAccessFile in = new RandomAccessFile(file,"rw");
        RandomAccessFile out = new RandomAccessFile(file,"rw");

        //继续,判断目标文件是否还存在,如果不存在,就从头复制,如果存在,
        //获取上次的断点位置,继续复制
        if (!file1.exists()){
            in.seek(0);
            out.seek(0);
        }
        BufferedReader br = new BufferedReader(new FileReader("config.txt"));
        String s = br.readLine();
        in.seek(Long.parseLong(s));
        out.seek(Long.parseLong(s));

        byte[] bytes = new byte[3];
        int len = 0;
        int i = 0;
        while ((len = in.read(bytes)) != -1){

            //模拟暂停
            /*i=i+=2;
            if (i > 100000){
                //获取暂停后文件的指针位置
                long fp = in.getFilePointer();
                //把这个文件指针的位置,保存到一个配置文件里面
                PrintWriter pw = new PrintWriter(new FileOutputStream("config.txt"),true);
                pw.println(fp);

                break;
            }*/
            out.write(bytes,0,len);
        }

        in.close();
        out.close();
    }

21.06_序列化流和反序列化流

  A:序列化流的概述
  		所谓的序列化:就是把对象通过流的方式存储到文件中,注意:此对象 要
  					 重写Serializable
  		反序列化:就是把文件中存储的对象以流的方式还原成对象
  		序列化流: ObjectOutputStream
  		反序列化流:	ObjectInputStream
public static void main(String[] args) throws IOException, ClassNotFoundException {
        //序列化:将内存中的java对象,保存文件
        //反序列化:将文件中的java对象,读取回内存中

        /*
            序列化流:
            ObjectOutputStream
            反序列化流:
            ObjectInputStream
         */

        //writeData();
        readData();



        //浅克隆:clone() 你克隆的这个对象,里面维护了另外一个类的对象,
        //		 你克隆时,他只会克隆你维护的那个对象的地址值,并不会把
        //		 你维护的那个对象克隆一份
        //深克隆:采用序列化流,来进行深克隆
    }

    private static void readData() throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("student.txt"));
        Student student = (Student) ois.readObject();
        String name = student.getName();
        int age = student.getAge();
        System.out.println(name);
        System.out.println(age);
    }

    private static void writeData() throws IOException {
        Student student = new Student("张三", 23);
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("student.txt"));

        //NotSerializableException 我们要把一个类的对象,要成功的序列化,
        //对这个类有一个要求,要求这个类必须实现一个 Serializable 这个序
        //列化接口
        oos.writeObject(student);
        oos.close();
    }
public static void main(String[] args) throws IOException, ClassNotFoundException {
        /*Student student1 = new Student("张三", 23);
        Student student2 = new Student("李四", 24);
        Student student3 = new Student("王五", 25);
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("student2.txt"));

        //NotSerializableException 我们要把一个类的对象,要成功的序列化,
        //对这个类有一个要求,要求这个类必须实现一个 Serializable 这个序
        //列化接口
        oos.writeObject(student1);
        oos.writeObject(student2);
        oos.writeObject(student3);
        oos.close();*/

        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("student2.txt"));
        Student student = (Student) ois.readObject();
        String name = student.getName();
        int age = student.getAge();
        System.out.println(name);
        System.out.println(age);
    }
public static void main(String[] args) throws IOException, ClassNotFoundException {
        /*Student student1 = new Student("张三", 23);
        Student student2 = new Student("李四", 24);
        Student student3 = new Student("王五", 25);
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("student2.txt"));
        ArrayList<Student> list = new ArrayList<>();
        list.add(student1);
        list.add(student2);
        list.add(student3);

        oos.writeObject(list);
        oos.close();*/

        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("student2.txt"));
        ArrayList<Student> list = (ArrayList<Student>) ois.readObject();
        Student student = list.get(2);
        System.out.println(student.getName());
        System.out.println(student.getAge());
    }

21.07_Properties的概述和作为Map集合的使用

  A:Properties的概述
  		Properties 表示一个持久的属性集。
  		Properties 可保存在流中或从流中加载。
  		属性列表中每个键及其对应值都是一个字符串。
  		Properties父类是Hashtable
  		— 属于双列集合,这个集合中的键和值都是字符串 Properties 不能指定
  		  泛型
  B:Properties的特殊功能
  		public Object setProperty(String key,String value)
  		public String getProperty(String key)
  		public Set<String> stringPropertyNames()
  C:Properties的load()和store()功能
  		Properties和IO流进行配合使用:
  		— public void load(Reader reader):读取键值对数据把数据存储到
  										   Properties中
        — public void store(Writer writer, String comments) 把Properties
        		集合中的键值对数据写入到文件中,comments注释
public static void main(String[] args) {
        // 属性集合 键值一般存储String类型
        //Properties
        Properties properties = new Properties();
        /*properties.put("username","张三");
        properties.put("password","123456");
        String username = (String) properties.get("username");
        System.out.println(username);*/

        //建议使用他自己特有的方法,放存取数据
        properties.setProperty("username","张三");
        properties.setProperty("password","123456");
        String username = properties.getProperty("username");
        String password = properties.getProperty("password");
        System.out.println(username);
        System.out.println(password);

        //参数2,默认值,当这个键没有找到对应的值,就返回默认值
        String password2 = properties.getProperty("passsword555", "56789");
        System.out.println(password2);
    }
public static void main(String[] args) throws IOException {
        //username = 张三
        //password = 123456
        LinkedHashMap<String,String> lhm = new LinkedHashMap<>();
        lhm.put("username","张三");
        lhm.put("password","123456");

        Set<Map.Entry<String, String>> entries = lhm.entrySet();
        BufferedWriter bw = new BufferedWriter(new FileWriter("user.properties"));
        for (Map.Entry<String, String> en : entries) {
            String key = en.getKey();
            String value = en.getValue();
            bw.write(key + "=" + value);
            bw.newLine();   //换行
            bw.flush();
        }
        bw.close();
    }
public static void main(String[] args) throws IOException {
        LinkedHashMap<String, String> lhm = new LinkedHashMap<>();
        BufferedReader br = new BufferedReader(new FileReader("user.properties"));
        while (true){
            String s = br.readLine();
            if (s == null){
                break;
            }
            String[] split = s.split("=");
            lhm.put(split[0],split[1]);
        }
        System.out.println(lhm);
    }
public static void main(String[] args) throws IOException {
        Properties properties = new Properties();

        //建议使用他自己特有的方法,放存取数据
        properties.setProperty("username","张三");
        properties.setProperty("password","123456");

        //把属性集合中的键值对数据,存储到文本文件中
        properties.store(new FileOutputStream("user2.properties"),null);  //默认注释为null
    }
public static void main(String[] args) throws IOException {
        //Properties 读取配置文件中的数据到集合中,这个配置文件的数据格
        //式,键值关系的数据,键和值用 = 拼接,文件后缀名.properties
        Properties properties = new Properties();
        properties.load(new FileReader("user2.properties"));
        System.out.println(properties);
    }

21.08_SequenceInputStream

  A:SequenceInputStream
  		表示其他输入流的逻辑串联。
  		它从输入流的有序集合开始,并从第一个输入流开始读取,直到到达文件
  		末尾,接着从第二个输入流读取,依此类推,直到到达包含的最后一个输
  		入流的文件末尾为止
  B:构造方法
  		SequenceInputStream(InputStream s1, InputStream s2)
  		通过记住这两个参数来初始化新创建的 SequenceInputStream(将按顺序
  		读取这两个参数,先读取s1,后读取s2),以提供从此 
  		SequenceInputStream 读取的字节
  		
  		SequenceInputStream(Enumeration < ? extends InputStream > e)
  			通过记住参数来初始化新创建的 SequenceInputStream,该参数必
  			须生成运行时类型为 InputStream 对象的 Enumeration 型参数
public static void main(String[] args) throws IOException {
        //SequenceInputStream 表示其他输入流的逻辑串联
        /*
            它从输入流的有序集合开始,并从第一个输入流开始读取,直到到达
            文件末尾接着从第二个输入流读取,依次类推,直到到达包含的最后
            一个输入流的文件末尾为止
         */

        FileInputStream in = new FileInputStream("user.properties");
        FileInputStream in1 = new FileInputStream("user2.properties");

        /*
            SequenceInputStream(InputStream s1, InputStream s2)
            通过记住这两个参数来初始化新创建的 SequenceInputStream(将
            按顺序读取这两个参数,先读取s1,然后读取s2)
         */

        SequenceInputStream sis = new SequenceInputStream(in, in1);

        FileOutputStream fos = new FileOutputStream("hehe.properties");

        byte[] bytes = new byte[1024];
        int len = 0;
        while ((len = sis.read(bytes)) != -1){
            fos.write(bytes,0,len);
            fos.flush();
        }
        sis.close();
    }
public static void main(String[] args) throws FileNotFoundException {
        FileInputStream in = new FileInputStream("user.properties");
        FileInputStream in1 = new FileInputStream("user2.properties");
        FileInputStream in2 = new FileInputStream("user2.properties");

        SequenceInputStream sis = new SequenceInputStream(in, in1);

        SequenceInputStream sis1 = new SequenceInputStream(sis, in2);
    }
public static void main(String[] args) throws IOException {
        /*
            SequenceInputStream(Enumeration < ? extends InputStream > e)
            继续记住参数来初始化新创建的 SequenceInputStream,该参数必须
            是生成运行时类型为 InputStream 对象的 Enumeration 型参数
         */

        FileInputStream in = new FileInputStream("user.properties");
        FileInputStream in1 = new FileInputStream("user2.properties");
        FileInputStream in2 = new FileInputStream("user2.properties");

        Vector<FileInputStream> vector = new Vector<>();
        vector.add(in);
        vector.add(in1);
        vector.add(in2);
        Enumeration<FileInputStream> elements = vector.elements();

        SequenceInputStream sis = new SequenceInputStream(elements);

        FileOutputStream fos = new FileOutputStream("hehe1.properties");

        byte[] bytes = new byte[1024];
        int len = 0;
        while ((len = sis.read(bytes)) != -1){
            fos.write(bytes,0,len);
            fos.flush();
        }
        sis.close();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在 Java 中将 PDF 文件转换为的一种常见方式是使用 `java.io` 包中的 `FileInputStream` 和 `ByteArrayOutputStream` 类。 以下是一个简单的示例代码,可以将指定的 PDF 文件转换为字节: ```java import java.io.*; public class PDFToStreamExample { public static void main(String[] args) { File file = new File("path/to/pdf/file.pdf"); try (FileInputStream fileInputStream = new FileInputStream(file); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) { byte[] buffer = new byte[1024]; int length; while ((length = fileInputStream.read(buffer)) != -1) { byteArrayOutputStream.write(buffer, 0, length); } byte[] pdfBytes = byteArrayOutputStream.toByteArray(); // 使用转换后的字节数组进行后续处理 // ... } catch (IOException e) { e.printStackTrace(); } } } ``` 在上面的代码中,首先创建了一个 `File` 对象,指定了要转换的 PDF 文件的路径。然后使用 `FileInputStream` 读取该文件的内容,并将其写入 `ByteArrayOutputStream`。最后,调用 `toByteArray` 方法获取字节数组表示的 PDF 数据。这个字节数组可以在后续处理中使用,例如写入到输出中,或者使用 PDF 解析库解析 PDF 内容。 ### 回答2: Java可以通过多种方式将PDF文件转换为。 一种常见的方法是使用Apache PDFBox库。PDFBox是一个开源的Java库,可以用于处理PDF文件。通过使用PDFBox,可以将PDF文件加载到内存中,然后将其转换为进行处理。 以下是使用PDFBox将PDF文件转换为的示例代码: ``` import org.apache.pdfbox.io.MemoryUsageSetting; import org.apache.pdfbox.io.RandomAccessBufferedFileInputStream; import org.apache.pdfbox.pdfparser.PDFParser; import org.apache.pdfbox.pdmodel.PDDocument; import java.io.IOException; import java.io.InputStream; public class PDFToStreamExample { public static void main(String[] args) { try { // 加载PDF文件 InputStream inputStream = new RandomAccessBufferedFileInputStream("path/to/pdf/file.pdf"); PDFParser parser = new PDFParser(inputStream); parser.parse(); // 获取PDDocument对象 PDDocument document = parser.getPDDocument(); // 将PDDocument对象转换为 document.saveToStream(System.out); // 关闭文档和输入 document.close(); inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 另外,如果你使用的是iText库,也可以使用类似的方式将PDF文件转换为。iText是一个用于创建和处理PDF文件的强大Java库。 值得注意的是,将PDF文件转换为后,可以根据需要将其保存到文件中,进行处理,或者发送到网络上。以上示例中,我们将直接输出到了标准输出。 ### 回答3: Java中可以使用Apache PDFBox库来将PDF文件转换为。下面是一个简单的示例代码: ```java import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.rendering.PDFRenderer; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import javax.imageio.ImageIO; public class PDF2StreamExample { public static void main(String[] args) { try { // 加载PDF文件 PDDocument document = PDDocument.load(new File("example.pdf")); // 创建PDF渲染器 PDFRenderer renderer = new PDFRenderer(document); // 将每个页面转换为图像,并将图像转换为字节数组 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); for (int i = 0; i < document.getNumberOfPages(); i++) { BufferedImage image = renderer.renderImageWithDPI(i, 300); ImageIO.write(image, "png", outputStream); } // 打印字节数组的大小 System.out.println("PDF转换为的大小:" + outputStream.size()); // 关闭文档及 document.close(); outputStream.close(); } catch (Exception e) { e.printStackTrace(); } } } ``` 以上代码将加载名为"example.pdf"的PDF文件,将其转换为图像,并将图像转换为字节数组。最后,输出字节数组的大小。 注意:在运行此示例之前,需要先将Apache PDFBox库添加到项目的依赖中。此外,还需要处理一些异常情况,以确保代码的健壮性和可靠性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值