convertfile.java
来自「Convert File 基本的 对文件的IO 操作 使用scanner 」· Java 代码 · 共 178 行
JAVA
178 行
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class convertFile {
String path = "C://Documents and Settings//QTIAHUA//Desktop//New Task//Convert File//input file2";
String prefix,mid1,mid2,suffix;
Scanner sa,sb;
FileWriter fw,fw2;
static FileWriter fw3;
static int beginPos = 11;
public String getOutFile3(String x,String y){
String result="";
String temp;
//x--old
Scanner s1 = new Scanner(x);
s1.useDelimiter("/");
s1.next();
result += s1.next();
temp = s1.next();
temp = temp.substring(temp.length()-5,temp.length());
result = result+" "+temp+" ";//CAAZA 107 2138 R1A15
//y--new
s1 = new Scanner(y);
s1.useDelimiter("/");
s1.next();
result += s1.next();
temp = s1.next();
temp = temp.substring(temp.length()-5,temp.length());
result = result+" "+temp;
System.out.println("3: "+result);
return result;
}
public String getOutFileSWR(Scanner st1,String inLine1,String inLine2,String name) throws IOException{
String outLine = "";
Scanner st2 = new Scanner(inLine2);
//两行新的和旧的ID都已经找到,并且分别赋给st1,st2(其中st1已经 next()一次)
//old 6 GAMGW C68 7PJB/CAAZA 107 2137/MQVAA R1A07 0 1 1 HI18059
//new 6 GAMGW C68 7000/CAAZA 107 2137/K R1A08 0 1 1 HI18059
outLine = st2.next()+" "+st2.next();//6 GAMGW
int olen = outLine.length();
for (int i = olen; i < 9; i++) outLine+=" ";
outLine = outLine+st2.next(); //6 GAMGW C68
String nnew = st1.next()+" "+st1.next()+" "+st1.next()+" "+st1.next();
//nnew=7000/CAAZA 107 2137/K R1A08
outLine = outLine +" "+nnew;
//6 GAMGW C68 7000/CAAZA 107 2137/K R1A08
//outLine = outLine +" "+st2.next()
String old = "";
for (int i = 0; i < 4; i++) old+=" "+st2.next();
//old.substring(1, old.length());
outLine = outLine +" "+st2.next();
while(st2.hasNext()){
outLine = outLine +" "+st2.next();
}
//6 GAMGW C68 7000/CAAZA 107 2137/K R1A08 0 1 1 HI18059
int tlen = name.length();
for (int i = tlen; i < 10; i++) name += " ";
System.out.println(tlen+" "+name);
fw3.write(name+getOutFile3(old,nnew)+"\r\n");
System.out.println("12: "+outLine);
return outLine;
}
public void getOutFile12() throws Exception{
try {
File out1 = new File(path+"//Prodax_Intermediate_SWR");
File out2 = new File(path+"//Final_Intermediate_SWR");
File out3 = new File(path+"//MA_Specification_CNA");
if(!out1.exists()) out1.createNewFile();
if(!out2.exists()) out2.createNewFile();
if(!out3.exists()) out3.createNewFile();
fw = new FileWriter(out1);
fw2 = new FileWriter(out2);
fw3 = new FileWriter(out3);
sa = new Scanner(new FileReader(path+"//block list"));
// FileReader fr = new FileReader(path+"//R____5AD0_C02______7621_UP.swr");
String inLine1,inLine2,outLine;
String temp;
Scanner st1;
while(sa.hasNextLine()){
inLine1 = sa.nextLine();
st1 = new Scanner(inLine1);
//find in input file 2
temp = st1.next();
inLine2="No Found!";
sb = new Scanner(new FileReader(path+"//R____5AD0_C04_CM08_7718.swr"));
while(sb.hasNextLine()){
inLine2 = sb.nextLine();
if(inLine2.indexOf(temp)!=-1){
if(inLine2.charAt(9)!=' '){
inLine2 = inLine2.substring(0,9)+" "+inLine2.substring(9);
}
break;
}
}
outLine = getOutFileSWR(st1,inLine1,inLine2,temp)+"\r\n";
fw.write(outLine);
fw2.write(outLine);
}
fw.close();
fw2.close();
fw3.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void getOutFile4() throws Exception{
try {
sa = new Scanner(new FileReader(path+"//block list"));
// File outFile = new File(path+"input file//Output");
// if(!outFile.exists()) outFile.mkdir();
//File4 output
File out4 = new File(path+"//MSC1.RUNLOG+.txt");
if(!out4.exists()) out4.createNewFile();
fw = new FileWriter(out4);
fw.write("\r\n");
//GATERM 7000/CAAZA 107 2138/T R1A15
//GATERM,000,,CAAZA,107,2138,R1A,15,
String inLine = "";
String outLine = "";
while(sa.hasNextLine()){
inLine = sa.nextLine();
Scanner st = new Scanner(inLine);
outLine = st.next()+","; //GATERM,
st.useDelimiter("/");
String temp4 = st.next();//String.valueOf(st.nextInt());
temp4 = temp4.substring(temp4.length()-3,temp4.length())+",,"; //GATERM,000,,
outLine =outLine + temp4 + st.next()+","; //GATERM,000,,CAAZA 107 2138
outLine = outLine.replaceAll(" ", ","); //GATERM,000,,CAAZA,107,2138
temp4 = st.next(); //T R1A15
int len = temp4.length();
temp4 = temp4.substring(len-5,len-2)+","+temp4.substring(len-2,len);
outLine = outLine + temp4+"";
fw.write(outLine+",\r\n");
System.out.println("4: "+outLine);
}
fw.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @param args
* @throws IOException
* @throws Exception
*/
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
try {
convertFile cf = new convertFile();
cf.getOutFile12();
cf.getOutFile4();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?