java基础之I/O操作
字节流
直接上代码:
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操作的更多相关文章
- Java基础之原生JDBC操作数据库
前言 日常开发中,我们都习惯了使用ORM框架来帮我们操作数据库,本文复习.记录Java如何使用原生JDBC操作数据库 代码编写 封装几个简单方法 find查询方法 findOne查询方法 update ...
- Java基础知识系列——目录操作
Java对目录操作的许多方法与上一篇文件操作的方法很多是一样的. java.io.File file = new File( "D:\1\2\3\4"); 1.递归创建目录 fil ...
- Java基础知识系列——文件操作
对文件进行操作在编程中比较少用,但是我最近有一个任务需要用到对文件操作. 对文件有如下操作形式: 1.创建新的文件(夹) File fileName = new File("C:/myfil ...
- Java基础知识之文件操作
流与文件的操作在编程中经常遇到,与C语言只有单一类型File*即可工作良好不同,Java拥有一个包含各种流类型的流家族,其数量超过60个!当然我们没必要去记住这60多个类或接口以及它们的层次结构,理解 ...
- java基础之数组常用操作
常用的对数组进行的操作 1.求数组中最大值,最小值 思路:假设下标为0的元素是最大值,遍历数组,依次跟max进行比较,如果有元素比这个max还大,则把这个值赋给max.最小值同样 public cla ...
- Java基础之cmd入门操作笔记
前提:jdk已安装且环境变量配置成功,参考上文jdk 安装及环境变量配置 入门操作步骤: 1.打开记事本或者notepad,编写Abc代码,具体如下: public class Abc{ pub ...
- android基础篇------------java基础(12)(多线程操作)
<一>基本概念理解 1.什么是进程? 进程就是在某种程度上相互隔离,独立运行的程序.一般来说,系统都是支持多进程操作的,这所谓的多进程就是让系统好像同时运行多个程序. 2.什么是线程呢? ...
- 【Java基础】ArrayList初始化操作
要用60个零初始化列表,请执行以下操作: List<Integer> list = new ArrayList<Integer>(Collections.nCopies(60, ...
- Java基础(命令行操作、注释及API、)
一.常用的dos命令. dir:列出当前目录下的文件及文件夹 md:创建目录 rd:删除目录 cd:进入到指定目录 cd..:退出到上一级目录 cd\:退出到根目录 del:删除文件 exit:退出d ...
随机推荐
- jrtp 使用
jrtplib-3.11.1 使用jthread-1.3.3 # cmake 下载https://cmake.org/download/ 使用地址https://github.com/j0r1/JRT ...
- PHP0007:PHP基础-字符串
php设置编码 用gbk编码识别utf8字符
- Jquery开发电商网站实战(带源码)
组件化思想,包含: 下拉菜单项封装 + 按需加载 搜索功能组件化,显示数据 + 下拉显示 + 缓存 分类导航按需加载 幻灯片效果组件封装及按需加载 商品楼层模块组件化 + 商品数据按需加载 + Tab ...
- list=null和list.size=0的区别
声明转载自:https://blog.csdn.net/iblade/article/details/50506398/ 转载自:https://blog.csdn.net/Hallelujah__/ ...
- PAT (Advanced Level) Practice 1054 The Dominant Color (20 分)
Behind the scenes in the computer's memory, color is always talked about as a series of 24 bits of i ...
- C++ lvalue(左值)和rvalue(右值)
lvalue(左值)和rvalue(右值) 昨天写代码遇见一个这样的错误:{ "cannot bind non-const lvalue reference of type 'int& ...
- Python查找列表中某个元素返回所有下标
需求 找出list中某一元素并返回所有匹配index值 问题 使用index()只能返回一个下标 >>> cw=[0,1,2,1,1,0,1,0,0,1] >>> ...
- 【Unity|C#】基础篇(15)——异常处理(try/catch/throw)
[学习资料] <C#图解教程>(第22章):https://www.cnblogs.com/moonache/p/7687551.html 电子书下载:https://pan.baidu. ...
- oracle建数据库
oracle用户界面登陆 用户要切换到oracle sqlplus / as sysdba //sys用户是oracle的最高管理员所以要加上as help index //查看命令列表,sql中不区 ...
- Luogu1287 | 盒子与球 (排列组合)
贴一个和其他题解不一样的做法 QWQ 题意:让我们求出 N 个球放入 R 个盒子且每个盒子都必须放球方案数. 首先,对于每一个球,可以将其放入的盒子数量共有 R 个,所以我们可以知道如果无需满足每个盒 ...
