文件out.txt中,有如下内容:0156
2 3 8 9
0483
1,4,5,9
6/ 5 1 、*。9要求:
编写程序,从out.txt中读取内容,经过处理后,显示
效果如下:0156、0156、0156
2389、2389、2389
0348、0348、0348
1459、1459、1459
1569、1569、1569
2 3 8 9
0483
1,4,5,9
6/ 5 1 、*。9要求:
编写程序,从out.txt中读取内容,经过处理后,显示
效果如下:0156、0156、0156
2389、2389、2389
0348、0348、0348
1459、1459、1459
1569、1569、1569
readline以后把读出来的String用toCharArray()方法转成char型数组,然后用Character的isDigit()方法把数字跳出来,然后调用Arrays的sort()方法就好了。
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;public class FileTest { public static void main(String[] args) {
File f = new File("F:/out.txt");
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(f));
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
try {
String str;
byte[] b = null;
while((str = br.readLine()) != null){
str = str.replaceAll("\\D", "");
b = str.getBytes();
Arrays.sort(b);
for(int i=0; i<3; ++i){
System.out.print(new String(b) + " ");
}
System.out.println();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.csdn;import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;public class HandleFile {
ArrayList<String> strLines;
File file = null;
PrintWriter pw = null;
public HandleFile(String path)
{
File file = new File(path);
if(file.exists())
{
this.file = file;
}
}
public void operateFile(File f) throws IOException
{
strLines = new ArrayList<String>();
if(file == null)
{
return;
}
FileInputStream fis = new FileInputStream(f);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String str;
while((str = br.readLine()) != null)
{
if(!"".equals(str.trim()))
{
strLines.add(str);
}
}
if(br != null)
{
br.close();
}
}
public String oprateLine(String line)
{
StringBuilder sb = new StringBuilder(0);
if(line == null)
{
return null ;
}
char[] chs = line.toCharArray();
ArrayList<Integer> numList = new ArrayList<Integer>();
for(int i = 0; i<chs.length; i++)
{
if(chs[i]>='0'&&chs[i]<='9')
{
numList.add(chs[i]-'0');
}
}
Collections.sort(numList);
Iterator<Integer> it = numList.iterator();
while(it.hasNext())
{
sb.append(it.next());
}
System.out.println(numList);
return sb.toString();
}
public void writeFile(File file, String data, int n) throws FileNotFoundException
{
if(file == null)
{
return;
}
if(pw == null)
{
pw = new PrintWriter(file);
}
for(int i=0; i<n; i++)
{
pw.print(data+(i==(n-1)?"":"、 "));
}
pw.print("\r\n");
}
public static void main(String[] args) throws IOException {
HandleFile hFile = new HandleFile("c:\\out.txt");
hFile.operateFile(hFile.file);
Iterator<String> it = hFile.strLines.iterator();
String line;
while(it.hasNext())
{
line = it.next();
String str = hFile.oprateLine(line);
hFile.writeFile(hFile.file, str, 3);
}
hFile.pw.flush();
if(hFile.pw !=null)
{
hFile.pw.close();
} }}请多指教!
然后用Arrays.sort排序就行了
//刚学不久
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileNotFoundException;class TestDemo
{ public static void main(String[] args) throws FileNotFoundException
{
Scanner in = new Scanner(new FileInputStream("D:\\out.txt"));
String [] str = new String[5];
int i=0;
while(in.hasNextLine())
{
str[i]=in.nextLine();
i++;
}
System.out.println("原out.txt内容:");
for(int a=0;a<i;a++)
{
System.out.println(str[a]);
}
String [] str1 = new String[i];
String s="";
for(int a=0;a<i;a++)
{
for(int b=0;b<str[a].length();b++)
{
if(str[a].charAt(b)>=48 && str[a].charAt(b)<=57)
s=s+str[a].charAt(b);
}
str1[a]=s;
s="";
}
System.out.println("");
System.out.println("处理后:");
for(int a=0;a<i;a++)
{
for(int b=0;b<2;b++)
{
System.out.print(str1[a]+",");
}
System.out.println(str1[a]);
}
}
}
//增加排序过程,
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileNotFoundException;class TestDemo
{ public static void main(String[] args) throws FileNotFoundException
{
Scanner in = new Scanner(new FileInputStream("D:\\out.txt"));
String [] str = new String[5];
int i=0;
while(in.hasNextLine())
{
str[i]=in.nextLine();
i++;
}
System.out.println("原out.txt内容:");
for(int a=0;a<i;a++)
{
System.out.println(str[a]);
}
String [] str1 = new String[i];
String s="";
for(int a=0;a<i;a++)
{
for(int b=0;b<str[a].length();b++)
{
if(str[a].charAt(b)>=48 && str[a].charAt(b)<=57)
s=s+str[a].charAt(b);
}
str1[a]=s;
s="";
}
System.out.println("");
System.out.println("提取数字后:");
for(int a=0;a<i;a++)
{
for(int b=0;b<2;b++)
{
System.out.print(str1[a]+",");
}
System.out.println(str1[a]);
}
//add: 排序处理
for(int a=0;a<i;a++)
{
char [] ch=new char[str1[a].length()];
ch=str1[a].toCharArray();
//冒泡排序
for(int m=0;m<ch.length;m++)
{
for(int n=0;n<ch.length-1-m;n++)
{
if(ch[n]>ch[n+1])
{
char temp;
temp=ch[n];
ch[n]=ch[n+1];
ch[n+1]=temp;
}
}
}
String s1="";
for(int b=0;b<ch.length;b++)
{
s1=s1+ch[b];
}
str1[a]= s1;
}
System.out.println("");
System.out.println("排序后:");
for(int a=0;a<i;a++)
{
for(int b=0;b<2;b++)
{
System.out.print(str1[a]+",");
}
System.out.println(str1[a]);
}
}
}
import java.io.*;
import java.util.*;public class TestSort
{
public static void main(String[] args) throws Exception{
final String path = "C:\\out.txt";
BufferedReader br = new BufferedReader(new FileReader(new File(path)));
String temp ;
while((temp = br.readLine()) != null)
{
temp = temp.replaceAll("\\D", "").trim();
char[] str = temp.toCharArray();
Arrays.sort(str);
for(int i = 0 ; i < 3 ; i++)
{
System.out.print(new String(str) + " ");
}
System.out.println();
}
br.close();
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class ReadFile {
public static void main(String[] args) {
File file=new File("d:\\out.txt");
readOper(file);
}
public static void readOper(File file)
{
BufferedReader br=null;
String str;
Pattern pattern=Pattern.compile("\\d");
List<String> list=new ArrayList<String>();
try {
br=new BufferedReader(new FileReader(file));
while((str=br.readLine())!=null)
{
Matcher matcher=pattern.matcher(str);
while(matcher.find())
{
String s=matcher.group();
list.add(s);
}
displayNum(list);
list.clear();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally
{
try {
if(br!=null)br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void displayNum(List<String> list)
{
Collections.sort(list);
for(int j=0;j<3;j++)
{
for(int i=0;i<list.size();i++)
{
System.out.print(list.get(i));
}
if(j<2)
System.out.print("、");
}
System.out.println();
}
}