字节流

直接上代码:

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. IP后面带/30 /29 /27等是什么意思?

    那个代表你网络的位数,也就是能判断子网掩码.比如30 说明就是11111111.11111111.11111111.11111100 (30个1,2个0)然后转换成十进制就是255.255.255.2 ...

  2. gulp常用插件之yargs使用

    更多gulp常用插件使用请访问:gulp常用插件汇总 yargs这是一款通过解析参数并生成优雅的用户界面来帮助您构建交互式命令行工具.处理命令行参数的通用解决方案,只要一句代码 var args = ...

  3. ubuntu set up 6 - NTFS Mount

    1. NTFS Mounted as read-only https://askubuntu.com/questions/1138076/ubuntu-18-04-cant-write-on-ntfs ...

  4. 服务器的公网ip 和内网ip

    原文地址:https://zhidao.baidu.com/question/814783729071869532.html 服务器公网ip 可以用于域名解析ip,服务器远程登录ip,是最主要的服务器 ...

  5. sqli-labs less-17 --> less-20

    Less-17 (报错盲注) 参考资料1:https://www.cnblogs.com/AmoBlogs/p/8673748.html

  6. 关于eclipse 项目导入不了 maven依赖的解决办法

    1.首先确定你的项目是maven 项目 ,如果不是:项目右键Configure -->Convert to maven project. 2.在SVN导出的Maven项目,或以前不是用Maven ...

  7. Java定时任务之Timer

    Timer是Java中实现定时任务的方式之一,下面是一个简单的例子: import java.util.Timer; import java.util.TimerTask; public class ...

  8. ControlTemplate in WPF

    Shared in all file window Button CheckBox Radiobutton Textbox ComboBox ListBox ItemsControl TreeView ...

  9. C#常规TcpListener

    1.Xaml <Window x:Class="Server.MainWindow" xmlns="http://schemas.microsoft.com/win ...

  10. Codeforces 577A - Multiplication Table

    Let's consider a table consisting of n rows and n columns. The cell located at the intersection of i ...