本帖最后由 洛漠O_o 于 2014-8-6 23:05 编辑
- public static void main(String[] args) throws IOException {
- BufferedReader freader = null;
- BufferedWriter fwriter = null;
- String path = ClassLoader.getSystemResource("com/itheima").toString();// 1.拿到文件的相对路径
- System.out.println(path);
- path = path.substring(5, path.length());// 2.对路径信息进行处理
- System.out.println(path);
- path = path.replace("bin", "src");
- System.out.println(path);
- File inFile = new File(path + "/a.txt");// 3.打开读取文件
- File outFile = new File(path + "/b.txt");// 4.打开输出文件
- freader = new BufferedReader(new FileReader(inFile));// 5.创建读取文件缓冲流
- fwriter = new BufferedWriter(new FileWriter(outFile));// 6.创建输出文件缓冲流
- while (freader.ready()) {
- String line = freader.readLine();// 拿到信息
- char[] ch = line.toCharArray();// 获取字符数组
- Arrays.sort(ch);// 排序
- System.out.println(ch);
- fwriter.write(new String(ch));// 输出排序后信息
- fwriter.flush();
- }
- fwriter.close();
- freader.close();
- }
复制代码
/*打印结果file:/E:/work/exam/bin/com/itheima
/E:/work/exam/bin/com/itheima
/E:/work/exam/src/com/itheima
*/
/*========================================*/代码添加以后,在编辑器里修改就出问题!在这里写了,
String path = ClassLoader.getSystemResource("com/itheima").toString();// 拿到包的路径
path = path.substring(5, path.length());// 对路径处理使其标准化
path = path.replace("bin", "src");//看你工程下,是有这两个文件夹的
|