程序学习啥的,多敲多想多思考就行啦!
最近又跟着重温学习了一遍IO流,虽然大多数都知道,也知道该怎么用,但过程中还是有很多收获的。另外还有能够多敲点东西!多练练!!!
【#案例相关#】
- 案例给定的数据:
001=蒙娜丽莎的眼泪
002=烟花易冷
003=上海滩
004=小苹果
005=夜空中最亮的星
- 功能实现要求:
-
项目根路径下创建songs.txt文件,文件中存储的数据以等号=分隔,左边代表歌曲的编号,右边代表歌曲名称,文件内容如下:
001=蒙娜丽莎的眼泪
002=烟花易冷
003=上海滩
004=小苹果
005=夜空中最亮的星 -
利用IO流读取songs.txt文件,将内容存入适当的Properties集合,并遍历打印集合内容,控制台输出如下格式内容:
例:[001]蒙娜丽莎的眼泪
-
模拟下载过程:假设在D:\盘中存在以上歌曲(有mp3格式、avi格式、mp4格式等,但是只要求下载mp3格式的),具体在哪个子目录中,不清楚(需要用到递归)
循环显示该菜单:请选择您要进行的操作:1:根据编号下载 2:下载以上所有歌曲 3:退出
举例:
用户输入:1
提示用户:请输入您要下载的歌曲编号:
用户输入:001
完成功能:将001编号对应的歌曲"蒙娜丽莎的眼泪",复制到E:\songs目录下(该目录如果不存在,则创建;如果存在则不创建)
用户输入:2
将以上所有歌曲复制到E:\songs目录下(该目录如果不存在,则创建;如果存在则不创建)
用户输入:3
退出系统 -
突然感觉自己实现的好像有点差别,哎。。。。。。不管了!先写完再整!!!
@【代码实现】:
package day10;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Properties;
import java.util.Scanner;
import java.util.Set;
/**
* 【此程序的功能实现:参考以上文件】
* @author lenovo
*/
public class TestIOSongs {
public static void main(String[] args) throws IOException {
// 1.指定内容创建songs.txt文件并返回
String fileName = "songs.txt"; // 存储歌曲信息的文本文件name
String fileContent = "001=蒙娜丽莎的眼泪\r\n002=烟花易冷\r\n003=上海滩\r\n" +
"004=小苹果\r\n005=夜空中最亮的星"; // 歌曲信息
String filePath = "E:\\eclipse_JC\\eclipse-workspace\\Java学习之路\\"
+ "src\\day09$day11_第9$11天操作文件夹\\a\\ab12"; // 创建的name文件和创建歌曲所存放的源路径
File file = TestIOSongs.createFile(filePath, fileName, fileContent); // 执行创建并返回file对象
// 2.利用流读取songs.txt文件并打印内容【格式:[001]蒙娜丽莎的眼泪】
Properties prop = TestIOSongs.readAndLog(file); // 返回file对象的Properties集合
Set<String> set = prop.stringPropertyNames(); // 返回Properties集合key的Set集合
String sElement = null; // Properties集合中的键值对对象
for (String string : set) { // 遍历输出打印指定格式的歌曲信息
sElement = "[" + string + "]_" + prop.getProperty(string);
System.out.println(sElement);
}
/* 3.模拟下载过程:假设在D:\\盘中存在以上歌曲(有mp3格式、avi格式、mp4格式等,但是只要求下载mp3格式的),
* 具体在哪个子目录中,不清楚(需要用到递归)循环显示该菜单:请选择您要进行的操作:1:根据编号下载 2:下载以上所有歌曲 3:退出
* 实现过程:
* 1>在指定路径下创建对应格式的歌曲文件
* 2>模拟下载(拷贝功能)
* >> 根据编号下载
* >> 下载以上所有歌曲
* >> 退出
*/
/** 1>在指定路径下创建对应格式的歌曲文件 */
ArrayList<String> types = new ArrayList<>();
Collections.addAll(types, ".mp3", ".avi", ".mp4", ".flv"); // 存储文件格式的集合
// 创建带有格式的歌曲文件
ArrayList<File> files = TestIOSongs.createMusicFiles(filePath, sElement, types, prop);
for (File file2 : files) {
System.out.println(file2);
}
/** 2>模拟下载(拷贝功能----对files进行处理(下午实现) */
String loadPath = "src\\day09$day11_第9$11天操作文件夹"
+ "\\a\\ab12\\删除\\模拟下载(拷贝)得到的文件"; // 歌曲下载(拷贝)的目标存储地址
System.out.println("请输入歌曲下载编号(1~5):");
@SuppressWarnings("resource")
int num = new Scanner(System.in).nextInt();
downLoadMusic(num, prop, filePath, loadPath); // 开始执行下载任务(单个任务)
System.out.println("以上程序运行正常!");
/** 3>模拟下载所有的".mp3"文件 */
File srcDir = new File(filePath);
String type = ".mp3";
File destDir = new File("E:\\eclipse_JC\\eclipse-workspace\\Java学习之路\\src\\"
+ "day09$day11_第9$11天操作文件夹\\a\\ab12\\删除\\模拟下载(拷贝)得到的文件\\下载所有.MP3文件");
copyAllFiles(srcDir, type, destDir); // 下载(拷贝)所有“.mp3”文件
System.out.println("程序运行结束!正在退出...");
}
/**
* 功能:<第一步使用>指定文件名字、格式和内容创建文本文件
* @param path 文件存储路径
* @param type 文件名称.格式(如:haha.java)
* @param content 文件内容的字符串表示
* @return 返回最终文件
* @throws IOException 异常抛出
*/
public static File createFile(String path, String nameType, String content) {
FileWriter fw = null;
String filePath = path + "\\" + nameType;
try {
fw = new FileWriter(filePath);
fw.write(content); // 写出字符串内容
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fw.flush();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return new File(filePath);
}
/**
* 功能:<第二步使用>利用字符流读取文件内容,存储到Properties集合中并返回
* @param file 目标对象
* @return pro 存有file内容的Properties集合
* @throws IOException
*/
public static Properties readAndLog(File file) throws IOException {
Properties pro = new Properties();
FileReader fr = new FileReader(file);
pro.load(fr);
return pro;
}
/**
* 功能:<第三步使用1>创建不同格式的歌曲文件
* @param path 创建的歌曲文件存放的路径
* @param sElement 歌曲文件名称(不带格式名)
* @param types 格式类型集合(格式名)
* @param prop 歌曲文件信息集合<K-V>
* @return 创建的所有歌曲文件file对象
*/
public static ArrayList<File> createMusicFiles(String path, String sElement, ArrayList<String> types, Properties prop) {
String sfile = null; // 文件路径名
ArrayList<File> files = new ArrayList<File>(); // 存放创建的歌曲文件file对象
int random = 0;
int arrSize = types.size();
Set<String> keySet = prop.stringPropertyNames();
for (String string : keySet) { // 遍历输出打印5个随机格式的歌曲信息
random = (int)(Math.random()*arrSize);
sElement = "[" + string + "]_" + prop.getProperty(string);
sfile = path + "\\" + sElement + types.get(random);
File file = new File(sfile);
try {
file.createNewFile();
files.add(file);
// 此if条件语句旨在增加".mp3"格式文件的数量(独立块可删除)
if (types.get(random).toLowerCase().equals(".mp3")) {
for (int i = 0; i < 2; i++) {
sfile = path + "\\" + sElement + i + types.get(random);
file = new File(sfile);
file.createNewFile();
files.add(file);
System.out.println(file);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
return files;
}
/**
* 功能:<第三步2使用>指定编号下载(拷贝)歌曲
* Descri:通过给定的数字num取出prop集合key对应的歌曲file对象进行下载到loadPath文件夹路径下
* @param num 手动输入的歌曲编号
* @param prop 歌曲信息集合
* @param filePath 歌曲源文件的存放地址
* @param loadPath 歌曲下载(拷贝)的存储目标地址
* @throws IOException
*/
public static void downLoadMusic(int num, Properties prop, String filePath, String loadPath) throws IOException {
File srcDir = null;
File destDir = null;
Set<String> set = prop.stringPropertyNames();
for (String string : set) {
if (num == Integer.parseInt(string)) {
srcDir = new File(filePath + "\\[" +string+"]_"+ prop.getProperty(string) + ".mp3");
destDir = new File(loadPath + "\\[" +string+"]_"+ prop.getProperty(string) + ".mp3");
//destDir.createNewFile();
System.out.println(destDir.getAbsolutePath());
TestIOSongs.copyOneFile(srcDir, destDir);
}
}
System.out.println("下载(拷贝)完成,请查看!!!");
}
/**
* 功能:<三2>从一个文件从一个文件夹中拷贝到另一份文件中(单一文件的拷贝)
* <调用说明> downLoadMusic(int num, Properties prop, String filePath, String loadPath)
* @param srcDir 源文件路径地址
* @param destDir 目标文件路径地址
* @throws IOException 异常声明
*/
public static void copyOneFile(File srcDir, File destDir) throws IOException {
//destDir.createNewFile();
FileInputStream fis = new FileInputStream(srcDir);
FileOutputStream fos = new FileOutputStream(destDir);
byte[] bt = new byte[20];
while (fis.read(bt) != -1) {
fos.write(bt);
fos.flush();
}
fis.close();
fos.close();
}
/**
* 功能:<暂未使用>给定key值遍历Properties集合,返回该key对应的value值
* @param prop
*/
public static String propKey2Value(String key, Properties prop) {
String value = null;
Set<String> set = prop.stringPropertyNames();
for (String str : set) {
if (key.equals(str)) {
value = prop.getProperty(str);
}
}
return value;
}
/**
* 功能:<第三步3使用>将该路径目录下“.mp3”格式的所有文件拷贝到另一份路径目录下。
* @param srcDir 源文件夹路径地址
* @param type 文本文件格式类型
* @param destDir 目标文件夹路径地址
* @throws IOException 异常声明
*/
@SuppressWarnings("unused")
public static void copyAllFiles(File srcDir, String type, File destDir) throws IOException {
File[] files = srcDir.listFiles();
for (File file : files) {
if (file.isFile() && file.getName().toLowerCase().endsWith(type)) {
// 调用文件拷贝程序执行拷贝功能
TestIOSongs.copyAllFile(file, new File(destDir + "\\" + file.getName()));
} else if (file.isDirectory()) {
File[] fileNext = file.listFiles();
copyAllFiles(file, type, destDir);
}
}
}
/**
* 功能:<三3>将内容从一个文件拷贝到另一份文件中(任意文件的拷贝功能)
* (应用内容:IO流)
* @param srcDir 源文件路径地址(有文件名)
* @param destDir 目标文件路径地址(有文件名)
* @throws IOException 异常声明
*/
@SuppressWarnings("unused")
public static void copyAllFile(File srcDir, File destDir) throws IOException {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(srcDir);
fos = new FileOutputStream(destDir);
int temp = 0;
byte[] bt = new byte[20];
while ((temp = fis.read(bt)) != -1) {
fos.write(bt);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
fos.flush();
fis.close();
fos.close();
}
}
/**
* 功能:使用字符输入流读取file文件内容,返回文件内容的字符串形式(仅仅用来测试用了,本题用不到 T_T)
* @param file 目标读取文件
* @return s 文件内容的字符串形式
*/
public static String readFile(File file) {
String s = null;
char[] ch = new char[4];
FileReader fr;
try {
fr = new FileReader(file);
while (fr.read(ch) != -1) {
s += new String(ch);
}
} catch (IOException e) {
e.printStackTrace();
}
return s;
}
}
/**
* 【总结:今天对于编写这个程序所遇到的三个问题】
* 1.在没有完全搞懂工作路径(路径自动拼接)的时候最好使用绝对路径,这样可以有效避免FileNotFoundException异常
* 2.对于抛出包含NullPointException异常的方法,尽量不要在方法执行的第一句给所要使用的流变量等赋值为Null
* 3.对于集合和数组,请在使用前将容器对象创建出来,不要觉得对其仅做了声明或者声明且赋Null值了就认为该容器对象就存在了,所有的容器在使用前都必须new出来!
* 否则NullPointException会在程序执行时等着折磨你!!!
* 4.((3)的拓展:)尤其是在抛大个异常<比如:Exception等>的方法中,所有的对象在使用前不要指定Null值,想要使用的对象都必须new出来并且赋值给相应的变量!
*/
☺☺☺就写到这啦 ,程序有错误了或者有更好的设计方案可以留言一起交流交流!!!☺☺☺ |
---|
【技术相关】:关于IO流的实用度多大,我不知道也不会去管,我喜欢的是实现的过程,自己能够做出来锻炼了自己的思维,那就是值得的。
学习见解 | <自学了三四个月,有SE基础了解点前端,但自学没有氛围和竞争,很容易选择安逸,所以还是接受了一直犹豫不定的想法!>(第16天记) |
---|---|
程序人生 | 现在每天就知道多敲多练,没别的事。以后自己能走到哪儿,到达哪个水平,我才懒得管,只要等到我工作了,能挣钱了,自己的心也就安定了不是! |