文件的读取与输出并排序 Java编程

假设文本文件abc1.txt中有一些数据,分为若干行,每行有一些个数不等的整数,用空格隔开。写一个程序读入该文件中的数据,对每行数据从小到大进行排序,然后按行输出到文本文件abc2.txt中,输出的文件中数据的格式与输入文件相同。下面是示例文件内容
12 56 89
13 32 132
121 223 56 89 13
124 56 98 123 213 56
3 56 2
6 54 56 89 213
参数里面的isAscend 用来决定是升序排列 还是降序排列:
/***
* 按最后修改时间排序皮枝
* @param list
* @param isAscend
*/
public static void sortListByTime(List<亮肢FileInfo> list , boolean isAscend) {
// 对ListView中数据燃键敏list排序
ComparatorByTime comparator = new ComparatorByTime(isAscend);
if (!list.isEmpty()) {
Log.e("sortListByTime()", "");
synchronized (list) {
Collections.sort(list, comparator);
}

}
}
/**
* 按文件大小排序
* @param list
*/
public static void sortListBySize(List<FileInfo> list , boolean isAscend) {
// 对ListView中数据list排序
ComparatorBySize comparator = new ComparatorBySize(isAscend);
if (!list.isEmpty()) {
Log.e("sortListBySize()", "");
synchronized (list) {
Collections.sort(list, comparator);
}

}
}
/**
* 按文件名称排序
* @param list
*/
public static void sortListByName(List<FileInfo> list , boolean isAscend) {
// 对ListView中数据list排序
ComparatorByName comparator = new ComparatorByName(isAscend);
if (!list.isEmpty()) {
Log.e("sortListByName()", "");
synchronized (list) {
Collections.sort(list, comparator);
}

}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class FileIO {

public static void main(String[] args) throws IOException {
/团含/ Read content from source
StringBuilder sb = new StringBuilder();
File file = new File("abc1.txt");
FileReader in = new FileReader(file);
BufferedReader reader = new BufferedReader(in);
while (reader.ready())
sb.append(reader.readLine() + " ");

/塌让笑/ Get string array
String[] arr = sb.toString().split(" ");

// convert to integer list
List<滑拦Integer> list = new ArrayList<Integer>();
for (String s : arr) {
if (s.equals("")) continue;
list.add(new Integer(s));
}

// sort list
Collections.sort(list);

// convert to String
sb = new StringBuilder();
for (int i : list)
sb.append(i + " ");

// Write to destination
File out = new File("abc2.txt");
if (!out.exists())
out.createNewFile();
FileWriter writer = new FileWriter(out);
writer.append(sb.toString());
writer.flush();
writer.close();
}

}
没加校验

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;

public class $ {
public static void main(String... _) {
try {
String str = getData("D:/裤好a.txt");

save("D:/b.txt"胡迹铅, str);
} catch (IOException e) {
e.printStackTrace();
}
}

private static String getData(String path) throws FileNotFoundException {
Scanner in = new Scanner(new File(path));
StringBuffer buf = new StringBuffer();

while (in.hasNextLine()) {
String str = in.nextLine();
String[] arr = str.split(" ");

Integer[] intArr = toInt(arr);

// 排序
Arrays.sort(intArr);
for (int i = 0; i < intArr.length; i++) {
buf.append(intArr[i] + " ");
}
buf.append("州塌\r\n");
}
return buf.toString();
}

private static Integer[] toInt(String[] arr) {
Integer[] a = new Integer[arr.length];

for (int i = 0; i < arr.length; i++) {
a[i] = Integer.parseInt(arr[i]);
}
return a;
}

private static void save(String path, String str) throws IOException {

FileWriter fw = new FileWriter(new File(path));

fw.write(str);
fw.flush();
fw.close();
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;

public class t {

/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
String fromPath="d:\\……\斗带\a1.txt";
FileReader f=new FileReader(fromPath);
BufferedReader br=new BufferedReader(f);
String line;
String[] strs;
String toPath="d:\\……\\空瞎芦a2.txt";
PrintWriter out=new PrintWriter(new BufferedWriter(new FileWriter(toPath)));
while((line=br.readLine())!=null)
{
strs=line.split(" ");
int[] datas = new int[strs.length];
for(int i=0;i<strs.length;i++)
datas[i]=Integer.parseInt(strs[i]);
Arrays.sort(datas);
for(int i=0;i<神稿datas.length;i++)
{
out.print(datas[i]+" ");
}
out.println();
}
out.close();
System.out.println("finish!");
}
}