2009年10月24日 星期六

範例二



class Mytestjava (Mytestjava.java)



import java.io.*;

import java.awt.*;

import javax.swing.*;

import java.util.StringTokenizer;

import java.nio.CharBuffer;

import java.util.Arrays;

import java.lang.Integer;;



public class Mytestjava2 {

static String[] array;

static String output = "";



// find value in array intValues

public static void printArrays(int[] intValues)

{

System.out.print( "doubleValues: " );



// for ( int count = 0; count < doubleValues.length; count++ )

// System.out.print( doubleValues[ count ] + " " );



System.out.print( "\nintValues: " );

output+= "\nintValues: ";

for ( int count = 0; count < intValues.length; count++ )

{

System.out.print( intValues[ count ] + " " );

output+= intValues[ count ] +" " ;

}

/*

System.out.print( "\nfilledInt: " );



for ( int count = 0; count < filledInt.length; count++ )

System.out.print( filledInt[ count ] + " " );



System.out.print( "\nintValuesCopy: " );



for ( int count = 0; count < intValuesCopy.length; count++ )

System.out.print( intValuesCopy[ count ] + " " );

*/

System.out.println();



}







public static int[]searchForInt(String[] inputarray, int max)

{

int [] temp=new int[max];

int kmax=0;

for (int i = 0; i < max; i++) {



try {

//System.out.println("inputarray("+i+")"+ inputarray[i]);

temp[i]=Integer.parseInt(inputarray[i]);

//System.out.println(""+temp[i]);

kmax=kmax+1;

} catch (Exception e) {

//System.out.println(""+inputarray[i]);

// XXX: handle exception

}

}



int [] ioutputarray=new int[kmax];

for (int k = 0; k < kmax; k++) {

ioutputarray[k]=temp[k];

}



return ioutputarray;

}





public static void main(String[] args) {

File position;

File text;

String InFileName,OutFileName;

int num;

String inputstr;

try{

InFileName = args[0];

StringTokenizer tokens = new StringTokenizer( InFileName, ".t");

position = new File(InFileName);

OutFileName = tokens.nextToken() + ".w" + tokens.nextToken();

System.out.print("Input File Name:" + InFileName + "\n" );

System.out.print("Output to the File :" + OutFileName+ "\n" );

text = new File(OutFileName);

}

catch(Exception e){

System.out.print("Error:"+e);

System.exit(1);

position = new File("");

text = new File("");

}

array = new String[20];



try{

BufferedReader input = new BufferedReader(new FileReader( position ) );

int i=0;

inputstr= input.readLine();

while (inputstr != null) {

array[i]= inputstr;

i=i+1;

inputstr= input.readLine();

}

input.close();

int max=i-1;

int[] Intmatrix= searchForInt(array, max);



for (int j = 0; j < Intmatrix.length; j++) {

//output+=array[i]+"\n" ;

System.out.println(""+Intmatrix[j]);

}



printArrays(Intmatrix);



}

catch(IOException IOException){

}





try{





BufferedWriter output1 = new BufferedWriter(new FileWriter( text) );

output1.write(output);

output1.flush();

}

catch(IOException IOException){

}

}

}






輸入檔案 930201.s01 內容:



1
2
3
4
5
6
8.4
9.3
0.2
7.9
3.4






執行結果:



D:\test2\mytest1\src>java Mytestjava2 930201.s01
Input File Name:930201.s01
Output to the File :930201.ws01
1
2
3
4
5
6




輸出檔案 930201.ws01 內容:





intValues: 1 2 3 4 5 6

沒有留言:

張貼留言