JAVA IO流 创建文件 读写字符串

本文详细介绍了如何使用Java IO流进行对象和字符串的读写操作。通过示例代码展示了如何将对象写入文件,从文件中读取对象,以及如何向文件写入和读取字符串。涉及的类包括FileOutputStream、ObjectOutputStream、FileInputStream、ObjectInputStream、FileWriter、BufferedWriter、FileReader和BufferedReader。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

JAVA IO流中对象,字符串的读写

package object;

import java.io.*;
import java.util.Date;

/**
* 名称:IO流中数据的读写
* 作者: 206工作室
* 时间:2009-12-27
* 版本:1.0
*/
public class ObjectOutTest {
/* 将对象写入文件方法 */
public void write(Object o, String path) {
   try {

    /* 创建存取文件 */
    FileOutputStream fileStream = new FileOutputStream(path);

    /* 将存取文件写入对象 */
    ObjectOutputStream os = new ObjectOutputStream(fileStream);

    /* 写入对象 */
    os.writeObject(o);

    System.out.println("写入数据成功");
    /* 关闭ObjectOutputStream */
    os.close();
   } catch (FileNotFoundException e) {
    e.printStackTrace();
   } catch (IOException e) {
    e.printStackTrace();
   }

}

/* 将对象从文件中读出来 */
public void read(String path) {

   try {
    /* 连接到要读取的文件 */
    FileInputStream fileStream = new FileInputStream(path);
    /* 怎样读取连接到的文件 */
    ObjectInputStream os = new ObjectInputStream(fileStream);
    /* 读取对象 */
    Dog dog = (Dog) os.readObject();

    System.out.println("输出结果:" + dog.getName() + " is a "
      + dog.getSex() + ",高:" + dog.getHeight() + ",长:"
      + dog.getLength());

    /* 关闭对象 */
    os.close();
   } catch (FileNotFoundException e) {
    e.printStackTrace();
   } catch (IOException e) {
    e.printStackTrace();
   } catch (ClassNotFoundException e) {
    e.printStackTrace();
   }

}

/* 往文件写入字符串 */
public void wirteString(String path, String context) {

   try {
    /* 创建写入对象 */
    FileWriter fileWriter = new FileWriter(path);
    /* 创建缓冲区 */
    BufferedWriter writer = new BufferedWriter(fileWriter);
    /* 写入字符串 */
    writer.write(context);
    /* 关掉对象 */
    writer.close();
    System.out.println("写入字符串成功!");
   } catch (IOException e) {
    e.printStackTrace();
   }
}

/* 读取文件中的字符串 */
public void readString(String path) {

   try {
    /* 创建读取对象 */
    FileReader fileReader = new FileReader(path);
    /* 创建缓存区 */
    BufferedReader reader = new BufferedReader(fileReader);
    /* 读取文件 */
    String line = null;
    while ((line = reader.readLine()) != null) {
     System.out.println("读取成功:" + line);
    }
    /* 关闭对象 */
    reader.close();
   } catch (FileNotFoundException e) {
    e.printStackTrace();
   } catch (IOException e) {
    e.printStackTrace();
   }

}

/* 出函数入口 */
public static void main(String args[]) {
   // Dog为外部类必须序列化才能实现它的对象的读取
   Dog d = new Dog();
   d.setHeight(12);
   d.setLength(23);
   d.setName("bobi");
   d.setSex('b');

   ObjectOutTest qqt = new ObjectOutTest();

   qqt.wirteString("F:/IO/cc.sex", "chengchao 123456789");
   qqt.readString("F:/IO/cc.sex");
}
}

public static String loadAFileToStringDE1(File f) throws IOException { long beginTime = System.currentTimeMillis(); InputStream is = null; String ret = null; try { is = new BufferedInputStream( new FileInputStream(f) ); long contentLength = f.length(); ByteArrayOutputStream outstream = new ByteArrayOutputStream( contentLength > 0 ? (int) contentLength : 1024); byte[] buffer = new byte[4096]; int len; while ((len = is.read(buffer)) > 0) { outstream.write(buffer, 0, len); } outstream.close(); ret = outstream.toString(); //byte[] ba = outstream.toByteArray(); //ret = new String(ba); } finally { if(is!=null) {try{is.close();} catch(Exception e){} } } long endTime = System.currentTimeMillis(); System.out.println("方法1用时"+ (endTime-beginTime) + "ms"); return ret; } public static String loadAFileToStringDE2(File f) throws IOException { long beginTime = System.currentTimeMillis(); InputStream is = null; String ret = null; try { is = new FileInputStream(f) ; long contentLength = f.length(); byte[] ba = new byte[(int)contentLength]; is.read(ba); ret = new String(ba); } finally { if(is!=null) {try{is.close();} catch(Exception e){} } } long endTime = System.currentTimeMillis(); System.out.println("方法2用时"+ (endTime-beginTime) + "ms"); return ret; } public static String loadAFileToStringDE3(File f) throws IOException { long beginTime = System.currentTimeMillis(); BufferedReader br = null; String ret = null; try { br = new BufferedReader(new FileReader(f)); String line = null; StringBuffer sb = new StringBuffer((int)f.length()); while( (line = br.readLine() ) != null ) { sb.append(line).append(LINE_BREAK); } ret = sb.toString(); } finally { if(br!=null) {try{br.close();} catch(Exception e){} } } long endTime = System.currentTimeMillis(); System.out.println("方法3用时"+ (endTime-beginTime) + "ms"); return ret; } 3个方法去读取一个大于50M的文件,当不设置jvm参数时都OutofMemery,当设置-Xmx128M时。只有方法3 可以通过,设置到-Xmx256M时也只有方法3可以通过,干脆设置512M,都可以了,运行时间如果正常的话一般都在4~5S
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值