java文件操作输入输出结构详解
作者:王小王_1 发布时间:2023-07-30 21:48:30
标签:java,文件,操作,输入,输出
一、实验目的
1. 掌握输入输出流的总体结构;
2. 掌握流的概念;
3. 掌握FileInputStream类、FileOutputStream类、FileReader类、FileWriter类的构造方法、常用方法的使用;
4. 了解各种流(包括文件流、管道流、连接文件、过滤流、对象的序列化、随机访问)的使用。
二、实验代码
1.使用Java的输入输出
使用Java的输入、输出流将一个文本文件的内容按行读出,每读出一行就顺序添加行号,并写入到另一个文件中。
package 作业练习.test4;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.io.File;
public class FileScanner {
public static void main(String[] args) throws Exception{
System.out.print("请输入文件名:");
Scanner reader = new Scanner(System.in);
String fileName = reader.nextLine();
File f = new File("E:\\Intellij IDEL\\project\\src\\"+fileName);
Scanner fi = new Scanner(f);
//输出:
String sLine = null;
int index = 0;
while(fi.hasNext()) {
sLine = fi.nextLine();
System.out.println(++index + " " + sLine);
try {
BufferedWriter out = new BufferedWriter(new FileWriter("test1.txt"));
out.write(index + " " + sLine);
} catch (IOException e) {
}
}
System.out.println("文件创建成功!");
}
}
2.使用RandomAccessFile流将一个文本文件倒置读出
package 作业练习.test4;
import java.io.*;
public class test2 {
public static void main(String []args) throws IOException
{
RandomAccessFile file =new RandomAccessFile("E:\\Intellij IDEL\\project\\src\\test4\\test.txt","r");
long len =file.length();
while(0!=len--)
{
file.seek(len);
char ch =(char)file.read();
System.out.print(ch);
}
file.close();
}
}
3.请分别使用不带缓冲区和带缓冲区的字节流复制图片(或者音频或者视频)文件
要求:
(1) 使用字节流FileInputStream、FileOutputStream实现复制;
(2) 在定义字节缓冲区大小时,可以尝试16字节、256字节、1024字节等不同大小的缓冲区进行复制。
(3) 请统计采用不同方式进行文件复制所花的时间,并进行分析。
package 作业练习.test4;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class App14_3 {
public static void main(String[] args) {
File reader = new File("E:\\Intellij IDEL\\project\\src\\test4\\1.png");
File writer = new File("\\Intellij IDEL\\project\\src\\test4\\2.png");
FileInputStream fis = null;
try {
fis = new FileInputStream(reader);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
BufferedInputStream bis = new BufferedInputStream(fis);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(writer);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BufferedOutputStream bos = new BufferedOutputStream(fos);
byte[] b = new byte[256];
int len = -1;
try {
while ((len = bis.read(b)) != -1) {
bos.write(b, 0, len);
bos.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bos.close();
fos.close();
bis.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package 作业练习.test4;
import java.io.*;
public class test3 {
public static void main(String []args) throws IOException
{
// 带缓冲区的字节流拷贝一个文本文件
FileInputStream fin =new FileInputStream("E:\\Intellij IDEL\\project\\src\\test4\\test.txt");
FileOutputStream fout =new FileOutputStream("E:\\Intellij IDEL\\project\\src\\test4\\test1.txt");
byte buf[] =new byte[2014]; //创建字节数组,作为临时缓冲
if(fin.read(buf)!=-1)
{
fout.write(buf);
}
System.out.println("文件复制成功");
fin.close();
fout.close();
/*带缓冲区的字符流拷贝一个文本文件
FileReader fin =new FileReader("E:\Intellij IDEL\project\src\test4\test2.txt");
BufferedReader din=new BufferedReader(fin) ;
FileWriter fou =new FileWriter("E:\Intellij IDEL\project\src\test4\test.txt");
BufferedWriter dou =new BufferedWriter(fou);
char c[]=new char[1024]; //创建字符数组
din.read(c);
fou.write(c);
System.out.println("文件复制成功");
din.close();
fou.close();
fin.close();
dou.close();
*/
}
}
4.请分别使用不带缓冲区和带缓冲区的字符流复制文本文件
要求:
(1) 使用字符流FileReader、FileWriter实现复制;
(2) 在定义字符缓冲区大小时,可以尝试16字符、256字符、1024字符等不同大小的缓冲区进行复制。
package 作业练习.test4;
import java.io.*;
public class App14_5 {
static App14_5 test=new App14_5();
public static String openFile(String fileName){ //打开文件
StringBuffer sb=null;
FileInputStream fis=null;
try {
fis=new FileInputStream(fileName); ; //实例化输入流对象
byte b[]=new byte[1024];
int len;
sb=new StringBuffer();
while( (len = fis.read(b))!=-1 ){ //读文件并判断是否到达文件尾
String str=new String(b,0,len);
sb.append(str);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
}
return sb.toString();
}
public static boolean saveFile(String fileName,String content) throws IOException{
boolean state=false;
FileOutputStream fos=null;
try {
fos=new FileOutputStream(fileName); //实例化输出流对象
//把content写入到文件中
state=true;
} catch (Exception e) {
e.printStackTrace();
}finally {
}
return state;
}
public static boolean copyFile(String sourceFileName,String destinationFifleName){
boolean sate =false;
InputStream fis=null;
OutputStream fos=null;
try {
fis=new FileInputStream(sourceFileName);
fos=new FileOutputStream(destinationFifleName);
int len;
byte buffer[]=new byte[1024];
//此处请填写多行
len=fis.read(buffer);
String str1=new String(buffer,0,len);
byte[] b = str1.getBytes();
fos.write(b);
sate =true;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(fis!=null) fis.close();
if(fos!=null) fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return sate;
}
public static void main (String args[]) {
App14_5 test=new App14_5();
test.copyFile("E:\\Intellij IDEL\\project\\src\\test4\\test.txt",
"E:\\Intellij IDEL\\project\\src\\test4\\test3.txt");
}
}
}
来源:https://blog.51cto.com/u_15172991/5428163
0
投稿
猜你喜欢
- 微服务治理Spring Cloud 工具套件为微服务治理提供了全面的技术支持。这些治理工具主要包括服务的注册与发现、负载均衡管理、动态路由、
- 我们都知道单精度浮点数(Single,float,Real)由32位0或1组成,它具体是如何来的。浮点数的32位N=1符号位(Sign)+8
- 先看代码public class MaxHuiWen {public static void main(String[] args) { &
- 已知两个链表list1和list,2,各自非降序排列,将它们合并成另外一个链表list3,并且依然有序,要求保留所有节点。实现过程中,lis
- 什么是JMMJMM全称Java Memory Model, 中文翻译Java内存模型,一种符合内存模型规范的,屏蔽了各种硬件和操作系统的访问
- 报错翻译: compileSdkVersion android-24”需要JDK 1.8或更高版本编译。报错现象如下图:原因:st
- webp格式图片webp格式图片是google推出的,相比jpg png有着巨大的优势,同样质量的图片webp格式的图片占用空间更小,在像电
- 第一种方法:string s=abcdeabcdeabcde;string[] sArray=s.Split(c) ;foreach(str
- 本文实例为大家分享了android自定义环形对比图的具体代码,供大家参考,具体内容如下1.首先在res/values里创建一个attr.xm
- 前言本文将带您了解在 Flutter 中制作翻转卡片动画的两个完整示例。第一个示例从头开始实现,第二个示例使用第三方包。闲话少说,让我们动手
- 相比于直线检测,直线拟合的最大特点是将所有数据只拟合出一条直线void fitLine( InputArray points, Output
- 前言不积跬步无以至千里,不积小流,无以成江海在公司一般来说,都只会接触一些CRUD的业务,很多时候可能你想设计很多的代码结构,但是时间不允许
- 最近碰到个需求,是希望在Unity有一个按钮,打开后直接跳转淘宝app,打开商品页面。百度了下没有相关的文章,于是我在此分享下。之前开发游戏
- 虽然说模块通信、路由协议在Android已经不新鲜了,但是如果脱离了那些优秀的开源库我们从零开始自己造一个库,有时候重复造轮子会让自己对所谓
- 1.在res上面右键->New->Android resource directory2.点击之后,出现下图Resource t
- android中提供了4中动画: AlphaAnimation 透明度动画效果 ScaleAnimation 缩放动画效果 Translat
- 这一篇我们说说Java线程Thread的interrupt中断机制。中断线程线程的thread.interrupt()方法是中断线程,将会设
- 今天来了一个问题:软键盘无法弹出。分析后是因为系统判断当前有外接硬键盘,就会隐藏软键盘。但实际情况并不是这么简单,该问题只有在特定条件下偶现
- 前言最近在工作中需要编译android下的动态库,本以为是一件简单的事,没想到因为工具,以及google本身被墙的原因,折腾了好久。在win
- 说到多渠道,这里不得不提一下友盟统计,友盟统计是大家日常开发中常用的渠道统计工具,而我们的打包方法就是基于友盟统计实施的。按照友盟官方文档说