字节流
//2012.8.9
//IO流操作
import java.io.*;
public class IoDemo{
public static void main(String args[]){
OutputStream out = null; //输出流
InputStream in = null; //输入流
//目录
String locket = "g:" + File.separator + "java"
+ File.separator + "do" + File.separator + "temp.txt";
//输出过程
try{
File f = new File(locket);
out = new FileOutputStream(f);
String wrt = "Hello World!!!Hello World!!!Hello " +
"World!!!Hello World!!!Hello World!!!Hello World!!!";
byte b[] = wrt.getBytes();//转换为byte数组类型
out.write(b);
System.out.println("写入完成!");
out.close();
}catch(Exception e){
e.printStackTrace();
}
//输入过程
try{
File f = new File(locket);
in = new FileInputStream(f);
byte b[] = new byte[in.available()];//定义biye数组 长度为文件大小
in.read(b);
String wrt = new String(b,0,b.length);
System.out.println(wrt);
in.close();
}catch(Exception e){
e.printStackTrace();
}
}
}这样的话会不会有长度限制??是不是只能达到1024?
本文详细介绍了Java字节流的基本使用,包括输出流和输入流的操作,通过实例演示了如何将字符串转换为字节并写入文件,以及从文件中读取字节并还原为字符串的过程,并探讨了操作过程中可能遇到的长度限制问题。
1525

被折叠的 条评论
为什么被折叠?



