字节流

直接上代码:

import java.io.*;

class Test{
public static void main(String[] args){
FileInputStream inputfile = null;
FileOutputStream outputfile = null;
try{
inputfile = new FileInputStream("./input.txt");
outputfile = new FileOutputStream("./output.txt");
byte[] buffer = new byte[100];
int temp = inputfile.read(buffer,0,buffer.length);
String s = new String(buffer);
s = s.trim();
System.out.println(s);
outputfile.write(buffer,0,temp); }
catch(Exception e){
System.out.println(e);
} }
}

优化版:

通过循环1M 1M读取文件

import java.io.*;

class Test{
public static void main(String[] args){
FileInputStream inputfile = null;
FileOutputStream outputfile = null;
try{
inputfile = new FileInputStream("./input.txt");
outputfile = new FileOutputStream("./output.txt");
byte[] buffer = new byte[1024];
while(true){
int temp = inputfile.read(buffer,0,buffer.length);
if(temp == -1){
break;
}
outputfile.write(buffer,0,temp); } }
catch(Exception e){
System.out.println(e);
}
finally{
try{
inputfile.close();
outputfile.close();
}
catch(Exception e){
System.out.println(e);
} } }
}

字符流

import java.io.*;

class TestString{
public static void main(String[] args){
FileReader inputfile = null;
FileWriter outputfile = null;
try{
inputfile = new FileReader("./input.txt");
outputfile = new FileWriter("./output.txt");
char[] buffer = new char[1024];
while(true){
int temp = inputfile.read(buffer,0,buffer.length);
if(temp == -1){
break;
}
outputfile.write(buffer,0,temp); } }
catch(Exception e){
System.out.println(e);
}
finally{
try{
inputfile.close();
outputfile.close();
}
catch(Exception e){
System.out.println(e);
} } }
}

BufferedReader.readLine

import java.io.*;

class TestBufferReader{
public static void main(String[] args){
FileReader fileReader = null;
BufferedReader bufferReader = null;
try{
fileReader = new FileReader("./input.txt");
bufferReader = new BufferedReader(fileReader);
String line =null;
while(true){
line = bufferReader.readLine();
if(line == null){
break;
}
System.out.println(line);
}
}
catch(Exception e){
System.out.println(e);
} }
}

java基础之I/O操作的更多相关文章

  1. Java基础之原生JDBC操作数据库

    前言 日常开发中,我们都习惯了使用ORM框架来帮我们操作数据库,本文复习.记录Java如何使用原生JDBC操作数据库 代码编写 封装几个简单方法 find查询方法 findOne查询方法 update ...

  2. Java基础知识系列——目录操作

    Java对目录操作的许多方法与上一篇文件操作的方法很多是一样的. java.io.File file = new File( "D:\1\2\3\4"); 1.递归创建目录 fil ...

  3. Java基础知识系列——文件操作

    对文件进行操作在编程中比较少用,但是我最近有一个任务需要用到对文件操作. 对文件有如下操作形式: 1.创建新的文件(夹) File fileName = new File("C:/myfil ...

  4. Java基础知识之文件操作

    流与文件的操作在编程中经常遇到,与C语言只有单一类型File*即可工作良好不同,Java拥有一个包含各种流类型的流家族,其数量超过60个!当然我们没必要去记住这60多个类或接口以及它们的层次结构,理解 ...

  5. java基础之数组常用操作

    常用的对数组进行的操作 1.求数组中最大值,最小值 思路:假设下标为0的元素是最大值,遍历数组,依次跟max进行比较,如果有元素比这个max还大,则把这个值赋给max.最小值同样 public cla ...

  6. Java基础之cmd入门操作笔记

    前提:jdk已安装且环境变量配置成功,参考上文jdk 安装及环境变量配置 入门操作步骤: 1.打开记事本或者notepad,编写Abc代码,具体如下: public class Abc{    pub ...

  7. android基础篇------------java基础(12)(多线程操作)

    <一>基本概念理解 1.什么是进程? 进程就是在某种程度上相互隔离,独立运行的程序.一般来说,系统都是支持多进程操作的,这所谓的多进程就是让系统好像同时运行多个程序. 2.什么是线程呢? ...

  8. 【Java基础】ArrayList初始化操作

    要用60个零初始化列表,请执行以下操作: List<Integer> list = new ArrayList<Integer>(Collections.nCopies(60, ...

  9. Java基础(命令行操作、注释及API、)

    一.常用的dos命令. dir:列出当前目录下的文件及文件夹 md:创建目录 rd:删除目录 cd:进入到指定目录 cd..:退出到上一级目录 cd\:退出到根目录 del:删除文件 exit:退出d ...

随机推荐

  1. 判定PDF文件是否能够正常打开

    下载iTextSharp.dll using iTextSharp.text.pdf; PdfReader reader = new PdfReader(strPath + "\\" ...

  2. C# DES加密、解密

    /// <summary> /// DES加密字符串 /// </summary> /// <param name="pToEncrypt">待 ...

  3. while语句的使用

    学习while语句的正确用法:题目:老师教学生,讲一次之后 问学生会不会,如果不会:就再讲一遍.如果会了就放学,但是如果连讲了十遍还是不会,那也要放学回家 namespace _44讲十遍会不会 { ...

  4. springboot web - 启动(4) tomcat

    接第二篇 第二篇里面, 看到容器创建的是 AnnotationConfigServletWebServerApplicationContext 类型. 一 .类图 二. 构造 public Gener ...

  5. Spring Cloud feign使用okhttp3

    指南 maven <dependency> <groupId>io.github.openfeign</groupId> <artifactId>fei ...

  6. rownum按某字段排序查询

    特点:rownum伪列,查询结果按顺序从1递增排列 用途:按某字段排序查询第几名到第几名的数据 但加上按字段排序条件后,rownum并不会从1递增 需把按字段排序查询的数据作为一张表,再次查询,row ...

  7. linux 第一个知识点(my)

    关于Linux - rwx rwx rwx root user 194 Oct : 19  21:24 test -:此符号为文件名,如果是d 则为目录,如果是l 则为连接 rwx: 这是所有者所有的 ...

  8. 【剑指Offer】02、替换空格

    题目描述 请实现一个函数,将一个字符串中的每个空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 题解:StringBuffer ...

  9. IntelliJ IDEA 如何彻底删除项目的步骤

    原文参考链接:https://www.jb51.net/article/129473.htm 本文介绍了IntelliJ IDEA 如何彻底删除项目的步骤,分享给大家,顺便给自己留个笔记,具体如下: ...

  10. webpack 代理问题

    devServer host: '0.0.0.0' 或者是ip 形式的 ,proxy 中 的 target,host 需要为ip形式的地址, host: 'aa.a.com' 为字符形式的 ,prox ...