file

file di testo e java
mail@pastecode.io avatar
unknown
java
2 years ago
3.4 kB
2
Indexable
Never
// file: BasicRWonFile.java

package basicfile;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

public class BasicRWonFile {
	public static void main(String[] args) throws Exception {
		final String NomeFile = "numericasuali.txt";
		File f = new File(NomeFile);
		if(!f.exists() || !f.isFile()){
			System.out.println("Creato file '" + NomeFile + "'..");
			f.createNewFile();
		}

		BufferedWriter fbw = new BufferedWriter(new FileWriter(f, true));
		fbw.write(System.currentTimeMillis()%1000 + "\n");
		fbw.close();

		BufferedReader fbr = new BufferedReader(new FileReader(f));
		for(String riga = ""; riga != null; riga = fbr.readLine()) {
			System.out.println(riga);
		}
		fbr.close();
	}
}

// file: ChatSave.java

package chatsave;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.FileReader;
import java.util.Scanner;

public class ChatSave {
	public static void main(String[] args) throws Exception {
		final String nomeFile = "chatsave.txt";

		File f = new File(nomeFile);
		if(!f.exists() || !f.isFile()) {
			f.createNewFile();
			System.out.println("creato file: '" + nomeFile + "'..");
		}
		BufferedWriter fbw = new BufferedWriter(new FileWriter(f,true));
		Scanner cin = new Scanner(System.in);
		String riga = "";
		System.out.println("Scrivi qualcosa, noi lo salveremo..");
		for( ; !riga.equalsIgnoreCase("end"); ) {
			System.out.print(">> ");
			riga = cin.nextLine();
			fbw.write(riga + "\n");
		}
		cin.close();
		fbw.close();

		System.out.println("*** Contenuto della chat ***");
		BufferedReader fbr = new BufferedReader(new FileReader(f));
		for(riga = "" ; riga != null; riga = fbr.readLine()) {
			System.out.println(riga);
		}
		fbr.close();
	}
}


// file: FibonacciLike.java

package fibonaccilike;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.FileReader;
import java.util.Scanner;

public class FibonacciLike {
	public static final String NOME_FILE = "fibonacciseriesLike.txt";

	public static File f = new File(NOME_FILE);

	public static boolean checkFile(final File f) throws Exception {
		return f.exists() && f.isFile();
	}

	public static void writeFile(File f, int n1, int n2) throws Exception {
		if(!checkFile(f)) {
			f.createNewFile();
		}
		BufferedWriter fbw = new BufferedWriter(new FileWriter(f,true));
		fbw.write(n1 + "\n");
		fbw.write(n2 + "\n");
		int n;
		for(int i = 0; i < 10; i++) {
			n = n1 + n2;
			fbw.write(n + "\n");
			n1 = n2;
			n2 = n;
		}
		fbw.close();
	}

	public static String readFile(File f) throws Exception {
		if(checkFile(f)) {
			String fileStr = "";
			BufferedReader fbr = new BufferedReader(new FileReader(f));
			for(String riga = ""; riga != null; riga = fbr.readLine()) {
				fileStr += riga + "\n";
			}
			fbr.close();
			return fileStr;
		}
		else return "";
	}

	public static void main(String[] args) throws Exception {
		Scanner cin = new Scanner(System.in);
		System.out.print("inserisci 2 interi: ");
		int
			start = cin.nextInt(),
			end = cin.nextInt();
		cin.close();
		writeFile(f, start, end);
		System.out.println(readFile(f));
	}
}