共回答了21个问题采纳率:100%
package image;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class LookingFile
{
public static final String FILE = "查找前.txt";
public static final String RESULT = "查找后.txt";
public static final String VALUES = "0,3,5,6,9";
public static Listlooking ( String file )
{
int index = -1;
String head = "", body = "";
String reg = "\s+";
ArrayListbucket = new ArrayList();
Scanner scanner = null;
try
{
scanner = new Scanner (new File (file), "UTF-8").skip ("[u4e00-u9fff\s]+");
while (scanner.hasNextLine ())
{
index++;
String[] line = scanner.nextLine ().split (reg);
head = line[0].trim ();
body = line[1].trim ();
if (VALUES.indexOf (body) != -1)
{
if (bucket.contains ("#"))
{
for ( int i = 0; i < bucket.size (); i++ )
{
if ("#".equals (bucket.get (i)))
{
bucket.set (i, String.valueOf (index - i));
}
}
}
}
if (!"0".equals (head))
{
bucket.add ("a");
}
else
{
bucket.add ("#");
}
}
for ( int i = 0; i < bucket.size (); i++ )
{
if ("#".equals (bucket.get (i)))
{
bucket.set (i, "空");
}
}
bucket.set (bucket.size () - 1, "No");
}
catch (Exception e)
{
e.printStackTrace ();
}
finally
{
if (null != scanner)
{
scanner.close ();
}
}
return bucket;
}
public static void writing ( Listbucket, String file, String result )
{
int index = -1;
String line = null;
FileReader fr = null;
BufferedReader br = null;
FileWriter fw = null;
BufferedWriter bw = null;
try
{
fr = new FileReader (file);
br = new BufferedReader (fr);
fw = new FileWriter (result);
bw = new BufferedWriter (fw);
while (null != ( line = br.readLine () ))
{
line = line.trim ();
if (line.matches ("\d+\s+\d+"))
{
index++;
bw.write (line + "tt" + bucket.get (index) + System.getProperty ("line.separator"));
}
}
bw.flush ();
bw.close ();
fw.close ();
br.close ();
fr.close ();
}
catch (Exception e)
{
e.printStackTrace ();
}
}
public static void main ( String[] args )
{
writing (looking (FILE), FILE, RESULT);
}
}
1年前
1