广告
返回顶部
首页 > 资讯 > 后端开发 > Python >java文件操作输入输出结构详解
  • 190
分享到

java文件操作输入输出结构详解

java文件操作输入java文件操作输出 2022-11-13 11:11:20 190人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

目录一、实验目的二、实验代码1.使用Java的输入输出2.使用RandoMaccessFile流将一个文本文件倒置读出3.请分别使用不带缓冲区和带缓冲区的字节流复制图片(或者音频或者

一、实验目的

  • 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();

}
}

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");
}
}
}

到此这篇关于java文件操作输入输出详解的文章就介绍到这了,更多相关java文件输入输出内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: java文件操作输入输出结构详解

本文链接: https://www.lsjlt.com/news/153773.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作