DataInputStream关闭的问题

tjone 2009-07-10 10:34:42
用DataInputStream 包装FileInputStream 如下:
FileInputStream fis=null;
try{
fis=new FileInputStream ("xxxxxx");
DataInputStream puts = new DataInputStream(fis);
.
.
.
}catch(FileNotFoundException e){
}finally{
try {fis.close();} catch (IOException e) {}finally{fis=null;}
}
最后关闭了了FileInputStream 对象,没有关闭DataInputStream 对象,请问会不会造成内存泄露,以至最终造成服务器内存溢出。
...全文
189 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
tjone 2009-07-10
  • 打赏
  • 举报
回复
自己顶一下
io流简介 File类 createNewFile() File.pathSwparator 与系统有关的路径分隔符,它被表示为一个字符串 windows为 分号";" File.pathSwparatorChar 与系统有关的路径分割符,它被表示为一个字符 File.separator 与系统有关的 默认名称分隔符 ,它被表示为一个字符串 windows为 斜杠"\" File.separatorChar 与系统有关的 默认名称分隔符 ,它被表示为一个字符 获取目录和文件 File.list() 返回String[] File.listFile() 返回File[] 字节流 FileInputStream("文件路径") FileOutputStream(File file) 逐个读取 存入字节read() write() close() 字符流 FileReader("文件路径") FileWriter("文件路径") 逐个读取 存入字符read() 无缓冲的输入、输出流每一次读写都肯引发磁盘的读写操作,占用大量资源 缓冲流(装饰器模式) 缓冲流是一种装饰器类 可实现按规定字符数、按行等方式的高效读写 缓冲区的大写可指定 也可使用默认大小 FileInputStream fis = new FileInputStream("Car.java"); 装饰器类 in = new 装饰器类(fis); BufferedReader in = new BufferedReader(new FileReader("Car.java")); BufferedWriter out = new BufferedWriter(new FileReader("Car2.java")); 利用缓冲流读取的时候是逐行读取 存入字符串 in.readLine() out.write("") out.newLine()写入分行符 需要即时写入的时候 调用 flush()方法,手动刷新缓冲流 注意 关闭流的时候也会自动刷新缓冲流中的数据 字节流转换为字符流 InputStreamReader(System.in) 适配器模式的使用 其意图是将一个类的接口转换成客户希望的另外一个接口 数据流 简单来说就是容许字节流直接操作基本数据类型和字符串 DataInputStream out = new DataInputStream(new BufferedInputStream(new FileInputStream("数据存储文件路径"))) DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("数据存储文件路径")))
实现本地电脑监控服务器端电脑监控功能 public class Client { // 入口 public static void main(String[] args) { try { int choice = JOptionPane.showConfirmDialog(null, "请求控制对方电脑", "远程控制系统-Charles", JOptionPane.YES_NO_OPTION); if (choice == JOptionPane.NO_OPTION) { return; } String input = JOptionPane.showInputDialog("请输入要连接电脑的ip(包括端口号)", "127.0.0.1:10000"); // 获取服务器的主机 String host = input.substring(0, input.indexOf(":")); // 获取服务器的端口号 String post = input.substring(input.indexOf(":") + 1); System.out.println("服务器的主机:" + host + " " + "端口号:" + post); Socket client = new Socket(host,Integer.parseInt(post)); DataInputStream dis = new DataInputStream(client.getInputStream()); JFrame jframe = new JFrame("本地监控系统 - Charles"); jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//默认关闭进程 jframe.setSize(1024, 768);// 设置窗体大小 double height = dis.readDouble(); double width = dis.readDouble(); Dimension ds = new Dimension((int)width, (int)height); //设置 jframe.setSize(ds); //将服务器图片作为背景 JLabel backImage = new JLabel(); JPanel panel = new JPanel(); //设置滚动条 JScrollPane scrollPane = new JScrollPane(panel); panel.setLayout(new FlowLayout()); panel.add(backImage); jframe.add(scrollPane); jframe.setAlwaysOnTop(true); jframe.setVisible(true); while(true){ int len = dis.readInt(); byte[] imageData = new byte[len]; dis.readFully(imageData); ImageIcon image = new ImageIcon(imageData); backImage.setIcon(image); jframe.repaint(); } } catch (Exception e) { e.printStackTrace(); } } }

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧