Advertisement
Guest User

java

a guest
May 24th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.96 KB | None | 0 0
  1. package Examen;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.util.Scanner;
  9.  
  10. public class fitxers {
  11.  
  12.     public static void main(String[] args) {
  13.         // TODO Auto-generated method stub
  14.        
  15.         FileOutputStream f=null;
  16.         Scanner sc=new Scanner (System.in);
  17.         System.out.println("Per finalitzar el fitxer, premer: '0'");
  18.         System.out.println("Introdueix el text");
  19.         String s=sc.nextLine();
  20.         char c=0;
  21.         System.out.println("En quin nom vol guardar el fitxer :");
  22.         String nom=sc.nextLine();
  23.         try {
  24.             f=new FileOutputStream(nom,false); //al posar true afegim carcaters al fitxer
  25.             for (int i=0; i<s.length();i++){
  26.                 c=s.charAt(i); //extraem el caracter guardat en la posicio i l'asignem a la variable c
  27.                 f.write(c); //convertim el caracter al tipus byte i l'escribim al fitxer.
  28.             }
  29.         }
  30.  
  31.         catch(IOException e){
  32.             e.printStackTrace();
  33.         }
  34.         finally{
  35.             try {
  36.             f.close();
  37.             }
  38.             catch (IOException e){
  39.                 e.printStackTrace();
  40.             }
  41.         }
  42.        
  43.  
  44.    
  45.     FileInputStream g=null;
  46.     String v= "";
  47.    
  48.     char b=0;
  49.     try {
  50.         g=new FileInputStream(nom);
  51.         int size=g.available();
  52.         for (int i=0; i<size; i++){
  53.             c=(char)g.read();
  54.             s=s+c;
  55.         }
  56.     }
  57.     catch (FileNotFoundException e){
  58.         System.out.println(e.getMessage());
  59.     }
  60.     catch (IOException e){
  61.         e.printStackTrace();
  62.     }
  63.     finally {
  64.         System.out.println(s);
  65.         try {
  66.             if (f!=null) f.close();
  67.         }
  68.         catch (IOException e){
  69.             e.printStackTrace();
  70.         }
  71.     }
  72.    
  73.     FileOutputStream h=null;
  74.     String j= "Texto de prueba que grabaremos en el fichero de datos ...\n";
  75.     char k=0;
  76.     try {
  77.         h=new FileOutputStream("paraula1.txt",true);
  78.         for (int i=0; i<s.length();i++){
  79.             c=s.charAt(i);
  80.             h.write(c);
  81.         }
  82.     }
  83.     catch(IOException e){
  84.         e.printStackTrace();
  85.     }
  86.     finally{
  87.         try {
  88.         h.close();
  89.         }
  90.         catch (IOException e){
  91.             e.printStackTrace();
  92.         }
  93.    
  94.  
  95.     }
  96.     sc.close();
  97. }
  98.    
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement