浏览 2442 次
锁定老帖子 主题:IO流读写txt中存储的文字信息和图片信息
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2011-07-30
public class Test { static File save = new File("save.txt"); /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { test1(); test2(); test3(); } //把文字信息写进TXT文件中 public static void test1() throws IOException { if (!save.exists()) save.createNewFile(); FileOutputStream output = new FileOutputStream(save); OutputStreamWriter output_writer = new OutputStreamWriter(output); PrintWriter pr = new PrintWriter(output_writer); pr.print(new Date()); pr.print("==============img"); pr.flush(); pr.close(); output_writer.close(); output.close(); } //把图片信息写进TXT文件中(在文字信息之后写) public static void test2() throws IOException { File file = new File("1.bmp"); if (!save.exists()) save.createNewFile(); FileInputStream input_stream = new FileInputStream(file); FileOutputStream out_stream = new FileOutputStream(save, true); int len = 0; byte[] bytes = new byte[1024]; while (-1 != (len = input_stream.read(bytes))) { out_stream.write(bytes, 0, len); } out_stream.close(); input_stream.close(); } //分别读去文字信息及图片信息 public static void test3() throws IOException { FileInputStream input = new FileInputStream(save); File temp = new File("temp.bmp"); if (!temp.exists()) temp.createNewFile(); FileOutputStream output = new FileOutputStream(temp); byte[] bytes = new byte[1]; int len = 0; while (-1 != (len = input.read(bytes))) { String temp1 = new String(bytes); System.out.println(temp1); if ("g".equals(temp1))//以g结束文字信息的读取 break; } byte[] bytes1 = new byte[1024]; int len1 = 0; //开始读取图片信息 while (-1 != (len1 = input.read(bytes1))) { output.write(bytes1, 0, len1); } output.close(); input.close(); } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |