Wednesday, March 3, 2010

Similar A Denis Milani

Esercizio sugli stream-svolto in classe

Given a list of name, date of birth of some people put in order verticale, posizionarle in modo orizzontale.


//per intercettare il tipo di errore input-output degli stream
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.io.FileNotFoundException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.io.BufferedWriter;
public class Stream {
public static void main(String[]args)throws IOException{
/*if(args.length!=4){
System.out.println("Usage: java CambiaFormato ");
System.exit(-1);
}
*/

if(args.length!=4){
String s="Usage:java CambiaFormato ";
throw new IllegalArgumentException(s);
}
FileInputStream FIn=null;
FileOutputStream FOut=null;
InputStreamReader inStream=null;
OutputStreamWriter outStream = null;
BufferedReader buffRead =null;
BufferedWriter buffWriter=null;
// null sta per vuoti
try{
FIn=new FileInputStream(args[0]);
Fout = new FileOutputStream (args [2]);

inStream = new InputStreamReader (FIN, args [1]);
outStream = new OutputStreamWriter (Fout, args [3]);

buffRead = new BufferedReader (inStream);
buffWriter = new BufferedWriter (outStream);
String temp = null, / / \u200b\u200btemporary string temp = one-time charge the nomr, then last name etc.
String name = null;
String name = null;
String data = null;
int count = 1, / / \u200b\u200bline to mention that I'm reading
while ((temp = buffRead.readLine ())!= null) {
if ((count% 3) == 1)
name = temp;

else if ((count% 3) == 2)
name = temp;


else {data = temp;
buffWriter.write (name + "" + surname +";"+ date + "\\ r \\ n");}

count + +;}

System.out.println ("Copy done, read" + (count-1) + "rows");
} catch (FileNotFoundException fnfe)
{System.out.println ("Error in id creation of input streams and / or output byte from / to file");
System.out.println(fnfe.getMessage());
}catch(UnsupportedEncodingException uee){
System.out.println("Codifiche dei flussi di input e/o di output di caratteri errate");
System.out.println(uee.getMessage());
}catch(IOException ioe){
System.out.println("Errore in fase di lettura e/o scrittura di caratteri");
System.out.println(ioe.getMessage());
}
finally{
if(buffWriter!=null)
buffWriter.close();
if(buffRead!=null)
buffRead.close();
if (outStream! = null)
outStream.close ();
if (buffWriter! = null)
buffWriter.close ();
if (buffWriter! = null)
buffWriter.close ();
if (buffWriter! = null)
buffWriter.close ();}


}}

0 comments:

Post a Comment