Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Examen;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.util.Scanner;
- public class fitxers {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- FileOutputStream f=null;
- Scanner sc=new Scanner (System.in);
- System.out.println("Per finalitzar el fitxer, premer: '0'");
- System.out.println("Introdueix el text");
- String s=sc.nextLine();
- char c=0;
- System.out.println("En quin nom vol guardar el fitxer :");
- String nom=sc.nextLine();
- try {
- f=new FileOutputStream(nom,false); //al posar true afegim carcaters al fitxer
- for (int i=0; i<s.length();i++){
- c=s.charAt(i); //extraem el caracter guardat en la posicio i l'asignem a la variable c
- f.write(c); //convertim el caracter al tipus byte i l'escribim al fitxer.
- }
- }
- catch(IOException e){
- e.printStackTrace();
- }
- finally{
- try {
- f.close();
- }
- catch (IOException e){
- e.printStackTrace();
- }
- }
- FileInputStream g=null;
- String v= "";
- char b=0;
- try {
- g=new FileInputStream(nom);
- int size=g.available();
- for (int i=0; i<size; i++){
- c=(char)g.read();
- s=s+c;
- }
- }
- catch (FileNotFoundException e){
- System.out.println(e.getMessage());
- }
- catch (IOException e){
- e.printStackTrace();
- }
- finally {
- System.out.println(s);
- try {
- if (f!=null) f.close();
- }
- catch (IOException e){
- e.printStackTrace();
- }
- }
- FileOutputStream h=null;
- String j= "Texto de prueba que grabaremos en el fichero de datos ...\n";
- char k=0;
- try {
- h=new FileOutputStream("paraula1.txt",true);
- for (int i=0; i<s.length();i++){
- c=s.charAt(i);
- h.write(c);
- }
- }
- catch(IOException e){
- e.printStackTrace();
- }
- finally{
- try {
- h.close();
- }
- catch (IOException e){
- e.printStackTrace();
- }
- }
- sc.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement