zoukankan      html  css  js  c++  java
  • java代码----------实现创建DataInputStream和DataOutputStream进行读写

    总结: 主要是 捕获异常

    package com.a.b;
    
    import java.io.*;
    
    public class testData {
    	public static void main(String[] args) throws IOException {
    		File newDir = new File("c:\javas");
    		if (!newDir.exists()) {
    			newDir.mkdir();// 如果目录不存在,就创建目录
    
    			File newFile = new File(newDir, "test.txt");
    			try {
    				newFile.createNewFile();
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}// 创建文件对象
    				// 建立DataInputStream 流
    			DataOutputStream dos = null;
    			try {
    				dos = new DataOutputStream(new FileOutputStream(newFile));
    			} catch (FileNotFoundException e1) {
    				// TODO Auto-generated catch block
    				e1.printStackTrace();
    			}
    			try {
    				dos.writeInt(10);
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}// 写入整型数据m
    			dos.writeBoolean(true);// 写入boolean型数据
    			dos.writeFloat(23.2f);// 写入浮点型double型
    			dos.writeChars("writeen into file");// 写入字符串
    			dos.close();
    			System.out.println("LIST ths file content");
    			// 建立DataInputStream流
    			DataInputStream dis = new DataInputStream(new FileInputStream(
    					newFile));
    			System.out.println(dis.readChar());
    			System.out.println(dis.readInt());
    			System.out.println(dis.readBoolean());
    			char c;
    			while ((c = dis.readChar()) != -1) {
    				System.out.println(c);// 读取字符串
    				dis.close();
    			}
    
    		}
    
    	}
    }
    

      

  • 相关阅读:
    python 全栈开发大纲
    2018/6/22 晚
    python——小知识
    变量与常量
    1.2数据结构-抽象数据类型的表示和实现
    1.1数据结构-基本概念和术语
    人机交互实践04-定位作业
    人机交互实践04-图像浮动至右边
    人机交互实践03-课堂作业2
    人机交互实践03-链接到的网页
  • 原文地址:https://www.cnblogs.com/langlove/p/3488438.html
Copyright © 2011-2022 走看看