Java 字节流实现文件读写操作(InputStream-OutputStream)

备注:字节流比字符流底层,但是效率底下。

字符流地址:http://pengyan5945.iteye.com/blog/1092354

package com.frank.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; /**
* author:pengyan
* date:Jun 15, 2011
* file:InputOutputStreamTest.java
*/
public class InputOutputStreamTest{
File f=new File("F:\\abc.txt");
public static void main(String[] args) throws Exception{
InputOutputStreamTest test=new InputOutputStreamTest();
test.outputFile("Java字节流读写文件测试!");
test.inputFile();
}
private void inputFile() throws IOException{
//reate FileInputStream with file
FileInputStream fis=new FileInputStream(f);
//in order to receive the value of this stream read every time
int temp=0;
//the all content of this stream read
String str="";
while((temp=fis.read())!=-1){
//if not end,the total content add the value of the stream read this time
str+=(char)temp;
}
//create a new String object with the correct encode
System.out.println("文件内容:"+new String(str.getBytes("ISO-8859-1")));
}
private void outputFile(String content) throws IOException {
if (f.exists()==false) {
f.createNewFile();//create file if not exist
}
//create FileOutputStream with file
FileOutputStream fos=new FileOutputStream(f);
//output file
fos.write(content.getBytes());
//flush this stream
fos.flush();
//close this stream
fos.close();
}
}

Java 字节流实现文件读写操作(InputStream-OutputStream)的更多相关文章

  1. java常用的文件读写操作

    现在算算已经做java开发两年了,回过头想想还真是挺不容易的,java的东西是比较复杂但是如果基础功扎实的话能力的提升就很快,这次特别整理了点有关文件操作的常用代码和大家分享 1.文件的读取(普通方式 ...

  2. Java 字符流实现文件读写操作(FileReader-FileWriter)

    Java 字符流实现文件读写操作(FileReader-FileWriter) 备注:字符流效率高,但是没有字节流底层 字节流地址:http://pengyan5945.iteye.com/blog/ ...

  3. java文件读写操作类

    借鉴了项目以前的文件写入功能,实现了对文件读写操作的封装 仅仅需要在读写方法传入路径即可(可以是绝对或相对路径) 以后使用时,可以在此基础上改进,比如: 写操作: 1,对java GUI中文本框中的内 ...

  4. [转]Android - 文件读写操作 总结

     转自:http://blog.csdn.net/ztp800201/article/details/7322110 Android - 文件读写操作 总结 分类: Android2012-03-05 ...

  5. Kotlin入门(27)文件读写操作

    Java的文件处理用到了io库java.io,该库虽然功能强大,但是与文件内容的交互还得通过输入输出流中转,致使文件读写操作颇为繁琐.因此,开发者通常得自己重新封装一个文件存取的工具类,以便在日常开发 ...

  6. Kotlin入门-文件读写操作

    转 https://blog.csdn.net/aqi00/article/details/83241762 Java的文件处理用到了io库java.io,该库虽然功能强大,但是与文件内容的交互还得通 ...

  7. 使用Java字节流拷贝文件

    本文给出使用Java字节流实现文件拷贝的例子 package LearnJava; import java.io.*; public class FileTest { public static vo ...

  8. Java字节流实现文件夹的拷贝

    import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io ...

  9. c语言文件读写操作总结

    C语言文件读写操作总结 C语言文件操作 一.标准文件的读写 1.文件的打开 fopen() 文件的打开操作表示将给用户指定的文件在内存分配一个FILE结构区,并将该结构的指针返回给用户程序,以后用户程 ...

随机推荐

  1. UIStoryboard

    UIStoryboard 目录 概述 Storyboard的创建 Storyboard中的页面跳转 文件内跳转 文件外跳转 Segues 不同类型的视图控制器在UIStoryboard上的实现 概述 ...

  2. javascript实现单例模式

    1.简单实现单例模式: var singleTon = function(){ var _pria = 'private value'; var show_pria = function(){ con ...

  3. ajax重写,js方法重新

    重写Jquery的$.ajax方法 (function($){ //备份jquery的ajax方法 var _ajax=$.ajax; //重写jquery的ajax方法 $.ajax=functio ...

  4. [拇指飞动]构建高性能Web站点(1)

    <构建高性能Web站点>中提到了Aphache, Nginx和lighthttpd. 把我的笔记share一下. 一般来讲Apache主要是基于多进程模型,早期的fork模式会为每一个re ...

  5. Creating Your Own Server: The Socket API, Part 1

    转:http://www.linuxforu.com/2011/08/creating-your-own-server-the-socket-api-part-1/ By Pankaj Tanwar  ...

  6. jQuery的如何捕捉回车键,改变事件标签

    我希望有一个jQuery的解决方案,我必须接近,有什么需要做的? $('html').bind('keypress', function(e) { if(e.keyCode == 13) { retu ...

  7. jQuery之防止冒泡事件

    冒泡事件就是点击子节点,会向上触发父节点,祖先节点的点击事件. 方法1: event.stopPropagation(); // 阻止事件冒泡 有时候点击提交按钮会有一些默认事件.但是如果没有通过验证 ...

  8. Boost.Any

    支持类型安全地存储和获取任意类型的值 #include <list> #include <boost/any.hpp> #include <string> #inc ...

  9. Shell学习笔记 - 环境变量配置文件

    一.source命令 功能:在当前bash环境下读取并执行配置文件中的命令 1. 命令格式 source 配置文件  或  . 配置文件 2. 命令示例 [root@localhost ~]# sou ...

  10. Wonderful Sentense

    1.Sorry if I might sound arrogant or offensive. 2.Any further question? 3.How dare you! 4.Try it if ...