黑马程序员-----练习

 ------- android培训java培训、期待与您交流! ----------

 /*已知文件a.txt文件中的内容为“bcdeadferwplkou”,

 * 请编写程序读取该文件内容,并按照自然顺序排序后输出到b.txt文件中。
 * 即b.txt中的文件内容应为“abcd…………..”这样的顺序。
 * 
 */


/**
 *  思路:
  1.创建一个List集合
 * 2.将原文件中的字母添加到集合中
 * 3.使用Collections工具类的sort方法 对List集合进行排序
 * 4.将集合中的元素写入目标文件中
 * 
 *
 */


/**
 * Test类
 * 实现题目需求
 * sortAndCopyFile()方法:将原文件中的内容排序后复制到目标文件中
 * write2File()方法:将排序后的List集合中的元素写入目标文件中
 * getList()方法:将原文件中的字母添加到集合中并对集合排序后返回集合对象。
 * getOutputStream():获取指定目标文件的输出流
 */
public class Test {

/**
* 将原文件中的内容排序后复制到目标文件中
* @param srcFilePath 原文件路径
* @param destFilePath 目标文件路径
*/
public static void sortAndCopyFile(String srcFilePath,String destFilePath) {
//获取对原文件内容排序后的List集合
List<Character> list = getList(srcFilePath);

//获取指定目标文件的输出流
OutputStream ops = getOutputStream(destFilePath);

//将集合中的元素写入目标文件中
write2File(list,ops);
}




/**
* 将原文件中的字母添加到集合中并对集合排序后返回集合对象。
* 实现步骤一,二,三
* @param srcFilePath 原文件路径
* @return 返回已排序的List集合
*/
private static List<Character> getList(String srcFilePath) {
FileInputStream fis =null;
BufferedReader br = null;

/*步骤一:创建一个List集合*/
List<Character> list = new ArrayList<Character>();;
try {
fis = new FileInputStream(srcFilePath);
br = new BufferedReader(new InputStreamReader(fis));
int b = -1;

while((b=br.read())!=-1) {
char ch = (char)b;
/*步骤二:将文件中的字母添加到集合中*/
list.add(ch);
}
} catch (FileNotFoundException e) {
new RuntimeException("文件没有找到!");
} catch (IOException e) {
new RuntimeException("文件读取失败!");
}finally {
try {
if(br!=null)
br.close();
} catch (IOException e) {
new RuntimeException("读取流关闭失败!");
}
}

/*步骤三:使用Collections工具类的sort方法 对List集合进行排序*/
Collections.sort(list);

return list;
}

/**
* 获取指定目标文件的输出流
* @param destFilePath 目标文件路径
* @return 返回指定目标文件的输出流
*/
private static OutputStream getOutputStream(String destFilePath) {
//创建目标文件对象
File file = new File(destFilePath);

//如果目标文件不存在,程序终止
if(!file.exists())
throw new RuntimeException("文件不存在");

OutputStream output= null;
try {
//获取指定目标文件的输出流
output = new FileOutputStream(file);
} catch (FileNotFoundException e) {
new RuntimeException("文件没有找到!");


return output;
}

/**
* 将集合中的元素写入目标文件中
* 实现步骤四
* @param list 对文件内容进行排序的集合
* @param ops  指定目标文件的输出流
*/
private static void write2File(List<Character> list,OutputStream ops) {
/*为了提高效率使用缓冲写入流*/
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(ops));

try {

//遍历List集合,也可使用迭代器,将List集合中的元素写入目标文件中
for(char ch : list) {
System.out.println(ch);
/*第四步:将集合中的元素写入缓冲写入流中,通过缓冲流的刷新写入到文件中*/
bw.write(ch);
}
} catch (IOException e) {
new RuntimeException("写入文件失败!");
}finally {
/*关闭流资源并刷新缓冲写入流,需要异常处理*/
try {
if(bw!=null)
bw.close();
} catch (IOException e) {
new RuntimeException("写入流关闭失败!");
}
}
}

/**
* 主函数进行功能测试
* 指定已知文件路径srcFilePath和目标文件路径destFilePath
* 调用sortAndCopyFile方法将已知文件内容按需求复制到目标文件中
*/
public static void main(String[] args) {
//定义已知文件路径
String srcFilePath = "src\\com\\itheima\\resources\\a.txt";

//定义目标文件路径
String destFilePath = "src\\com\\itheima\\resources\\b.txt";

//功能测试,已知文件内容按需求复制到目标文件中
sortAndCopyFile(srcFilePath,destFilePath);
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值