简单的输入输出流(2)
来源:互联网 发布:ar口袋动物园软件 编辑:程序博客网 时间:2024/06/07 15:24
从一个文本文件中读取数据
public class FileInputStreamDemo1 { /** * 使用InputStream抽象类的子类FileInputStream类 * 从一个文件中读取流数据 */ public static void test1() { try { FileInputStream myStream = new FileInputStream("src/a.txt"); int a = 0; while((a=myStream.read())!=-1){ //从a.txt读取内容一直到文件的末尾 System.out.print((char)a); } myStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { test1(); }}
简单的输出流
输入一个字符到文本中
public class Demo1 { /** * 输入一个字符到文件中 */ public static void test1() { try { OutputStream os = new FileOutputStream("src/b.txt"); os.write(97); os.flush(); os.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
运行结果为a,因为ascii码97对应的字符为‘a’;输出一个数组/** * 输出一个数组 */ public static void test2() { byte myArray[] = new byte[5]; myArray[0]=97; myArray[1]=98; myArray[2]=99; myArray[3]=100; myArray[4]=101; try { OutputStream os = new FileOutputStream("src/b.txt"); os.write(myArray); os.flush(); os.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
输出结果为abcde
输出字符串
public static void test3() { String myContent = "I am a good man!"; try { OutputStream os = new FileOutputStream("src/b.txt"); os.write(myContent.getBytes()); os.flush(); os.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
运行结果为:I am a good man! 。
将一个文件的内容输出到另一个文件中
public static void copy(){ try { FileInputStream myStream = new FileInputStream("src/1.png"); OutputStream os = new FileOutputStream("2.jpg"); int a = 0; while((a = myStream.read())!=-1){ //一直读到文件的末尾 os.write((char)a); } os.flush(); os.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
0 0
- 简单的输入输出流(2)
- 输入输出流(字符)的简单实例
- 简单的输入输出流(1)
- IO流的简单输入输出
- C中简单的输入输出流
- java中输入输出流的简单应用
- C#中一个简单的输入输出流
- 文件输入输出流的简单用法
- 简单的输入输出
- 简单的输入输出问题
- 最简单的输入输出
- 简单的输入输出交互
- 汇编简单的输入输出
- 简单的输入输出
- 简单的输入输出
- python002简单的输入输出
- 输入输出流简单理解
- 3-2 简单输入输出
- 对mtk平台的一些认识
- servlet笔记_01
- shell脚本中的冒号作用
- 图像处理基础(1):噪声的添加和过滤
- 细谈I2C和SPI总线协议
- 简单的输入输出流(2)
- Servlet_03
- 33.activity的四种启动模式
- Java泛型(Generic)
- mysql5.7安装教程(windows)
- 接受外来图片数据,创建Mat矩阵的几种方法
- Javascript中的闭包及应用
- python笔记-string
- Laravel学习:视图变量传递和Blade