您的位置:

java.io,javaio是什么包

本文目录一览:

java swing 画图怎么保存?求详细代码

package test;

import javax.swing.*;

import javax.imageio.ImageIO;

import java.awt.*;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

public class TestJLabel2Image {

public static void main(String ds[]) {

final JFrame f = new JFrame();

JPanel panel = new JPanel(new BorderLayout());

JLabel label = new JLabel() {

public void paintComponent(Graphics g) {

g.setColor(Color.red);

g.fillOval(0, 0, this.getWidth(), this.getHeight());

g.dispose();

}

};

panel.setPreferredSize(new Dimension(555, 555));

panel.add(label, BorderLayout.CENTER);

f.getContentPane().add(panel);

f.pack();

BufferedImage image = createImage(panel);

f.dispose();

try {

ImageIO.write(image, "png", new File("d:/test.png"));

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public static BufferedImage createImage(JPanel panel) {

int totalWidth = panel.getPreferredSize().width;

int totalHeight = panel.getPreferredSize().height;

BufferedImage panelImage = new BufferedImage(totalWidth, totalHeight, BufferedImage.TYPE_INT_RGB);

Graphics2D g2D = (Graphics2D) panelImage.createGraphics();

g2D.setColor(Color.WHITE);

g2D.fillRect(0, 0, totalWidth, totalHeight);

g2D.translate(0, 0);

panel.paint(g2D);

g2D.dispose();

return panelImage;

}

}

java.io.IOException是怎么回事

IOException的故事

1. 什么是IOException 

这个你可以看看API 

 

2. 广泛的说,什么时候会有IOException 

比如你文件都不到的时候 

你在做数据库操作的时候数据库底层出现问题 

或者你系统IO出问题了 

系统拿不到文件句柄 

你说的读着读着突然被删了,你可以试试,书不定真可以 

你可以看有多少IOExeption个子类,差不多就有多少种类型 

3. 为什么我要捕获IOExeption 

为什么要有checked exception,这个是java设计的问题,暂不深究 

但是这个IOException的意思就是告诉你,万一你在做io操作的时候出现异常怎么办 

最简单的例子是,我

Class clazz = Class.forname("/path/to/class");

这个时候万一找不到这个class文件该怎么办,也算提醒程序员极有可能出现问题的地方,这里不能忽略 

还有一个例子是

try {  

    stream.close()  

} catch(IOException e) {  

    // ignore  

}

你可以看到这里我们的IOException是忽略的,因为关闭不了,我们也没办法 -_-!!!

java中的import java.io.*是什么意思 io具体又是什么意思??

import java.io.* 这个是引用包import java.io.*这个的意思而IO则是输入输出流的意思,也就是inputStream,和outputStream这些类的