package com.ffyc.javaio.File.Stream;
import java.io.*;
public class FileOper {
public static void main(String[] args) {
try{
cutFile(new File("D:\非凡英才\HJ2204\2022-7-11_java第八章IO\练习\feige.exe"),1024*1024);
mergeFile(new File("D:\temp"));
}catch(FileNotFoundException e){
e.printStackTrace();
System.out.println("文件找不到");
}catch (IOException e){
e.printStackTrace();
System.out.println("读写异常");
}
}
private static void mergeFile(File file) throws IOException {
String[] array= file.list();
FileOutputStream out= new FileOutputStream("D:/feige");
for (int i = 0; i < array.length; i++) {
FileInputStream in=new FileInputStream("D:/temp/"+(i+1)+".temp");
byte[] b=new byte[1024];
int size=0;
while((size=in.read(b))!=-1){
out.write(b,0,size);
}
in.close();
}
out.close();
//删除文件
}
private static void cutFile(File file, int i) throws IOException {
FileInputStream in=null;
FileOutputStream out=null;
try{
in=new FileInputStream(file);
//创建临时缓存文件夹
File f=new File("D:/temp");
if(!f.exists()){
f.mkdir();
}
int n=1024*1024;
byte[] b=new byte[n];//1024*1024 一份200MB
int size=0;
int i1=1;
while((size= in.read(b))!=-1){
out=new FileOutputStream("D:/temp/"+i+++".temp");
out.write(b,0,size);
}
} finally {
if(in!=null){
in.close();
}
if(out!=null){
out.close();
}
}
}
}
2.实现注册,登录。 输入1 进入注册功能,输入2进入登录功能,输入3退出
用户信息:用户名(userName),密码(password) (1)注册功能:输入自己的个人信息,保存到文件。 (2)登录:输入自己的个人信息,与文件中的每行用户信息比较,判断登录是否成功.
package com.ffyc.javaio.File;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class HMMap {
public static void main(String[] args) throws IOException, ClassNotFoundException {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("1-----注册-----");
System.out.println("2-----登录-----");
System.out.println("3-----退出-----");
int i = scanner.nextInt();
if (i == 1)
HMMap.zhuce();
if (i == 2)
HMMap.login();
if (i == 3)
break;
}
}
private static void zhuce() throws IOException {
//注册
Scanner scanner = new Scanner(System.in);
Map
for (int i = 0; i < 5; i++) {
System.out.println("输入7 继续 ;输入其他数字 终止");
int a = scanner.nextInt();
if (a == 7) {
System.out.println("请输入注册的账号");
String s1 = scanner.next();
System.out.println("请输入注册的密码");
String s2 = scanner.next();
map.put(s1, s2);//将账号密码存到map里面
} else break;
}
FileOutputStream out = new FileOutputStream("D:\2219851498\账号密码.txt");
ObjectOutputStream Wout = new ObjectOutputStream(out);
Wout.writeObject(map);//将map流入到磁盘
out.flush();//刷新流
out.close();//关闭流
}
private static void login() throws IOException, ClassNotFoundException {
//登录
Scanner scanner=new Scanner(System.in);
FileInputStream reader = new FileInputStream("D:\2219851498\账号密码.txt");
ObjectInputStream oin = new ObjectInputStream(reader);
Map
System.out.println("请输入账号");
String z = scanner.next();
System.out.println("请输入密码");
String m = scanner.next();
if (!map.containsKey(z)) {//判断map里面有没有输入的键
System.out.println("账号错误或查无此账号");
}
else{
if(map.get(z).equals(m))
{
System.out.println("登陆成功");
}
else System.out.println("密码错误");
}
}
}
3.有一个文件(文件名自定义)中有一顺序打乱的诗
3. 乱石穿空,惊涛拍岸,卷起千堆雪。
2. 故垒西边,人道是,三国周郎赤壁。
4. 江山如画,一时多少豪杰。
1. 大江东去,浪淘尽,千古风流人物。
7. 故国神游,多情应笑我,早生华发。
8. 人生如梦,一尊还酹江月。
5. 遥想公瑾当年,小乔初嫁了,雄姿英发。
6. 羽扇纶巾,谈笑间,樯橹灰飞烟灭。
请你写程序排好序输出到文件中
package com.ffyc.javaio.File;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
public class poem {
public static void main(String[] args) throws IOException {
//这个用来读
FileReader reader=new FileReader("D:\poem\新建文本文档.txt");
//这个用来写
FileWriter writer = new FileWriter("D:\poem\新建文本文档1.txt");
BufferedReader reader1=new BufferedReader(reader);
BufferedWriter bufferedWriter = new BufferedWriter(writer);
String line=null;
//这个集合用来存储古诗,便于排序
ArrayList
while((line=reader1.readLine())!=null){
//读一行存一行
arrayList.add(line);
}
//Collections,排序
Collections.sort(arrayList);
for (String s: arrayList) {
bufferedWriter.write(s);
bufferedWriter.newLine();
bufferedWriter.flush();
}
System.out.println(arrayList);
bufferedWriter.close();
}
}
4.IO体系结构
5缓冲处理流的实现原理
对大文件进行读写操作,缓冲流比普通流效率高很多,是由于缓冲流会在内存中会开辟缓冲区,算是用空间换时间。比如BufferedInputStream的读操作,如果输入缓冲区没有数据,BufferedInputStream会一次性读取默认缓冲区大小的数据比如读取默认大小8192个字节存放在输入缓冲区,然后用FileInputStream进行读操作,同样BufferedOutputStream的写操作,首选是通过FileOutputStream将字节写入到输出缓冲区,然后等达到缓冲区默认大小时再一次性将缓冲区默认大小的数据比如将默认大小8192个字节一次写入到指定文件中,有了缓冲区后,可以大大减少IO次数。
暑期编程PK赛 得CSDN机械键盘等精美礼品!


