example8_2.java

来自「书中的例题」· Java 代码 · 共 42 行

JAVA
42
字号
/* 读写图像文件 */
import javax.swing.JOptionPane;
import java.io.*;
class FileRW 
 { 
   int    bytes;
   byte   buffer[ ] = new byte[65560];
   FileInputStream    fileInput;
   FileOutputStream   fileOutput;

   FileRW()
	{
      takeimg();
	  loadimg();
      JOptionPane.showMessageDialog(null,"文件复制并更名成功!\n文件大小为:"+bytes);
      System.exit(0);     //退出程序
    }
     //读取图像文件a.jpg
   public void takeimg() 
	{
	  try {
			File file=new File("a.jpg");
			fileInput = new FileInputStream(file);
            bytes = fileInput.read(buffer,0,65560);
          } catch(IOException ei) {   System.out.println(ei); }
    }
     //写入到b.jpg
   public void loadimg()
	{
	  try {
			fileOutput = new FileOutputStream("b.jpg");
            fileOutput.write(buffer, 0, bytes) ; 
	      } catch(IOException eo) { System.out.println(eo);   }
	 }
  }

public class Example8_2
{
    public static void main(String[] args) 
 	 { new FileRW();	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?