//LEGGE UN FILE
File descFile = new File("C:/TMP/Db visit desc.csv");
try {
Scanner scanner = new Scanner(descFile);
while (scanner.hasNextLine()){
String line = scanner.nextLine();
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Prova_1.class.getName()).log(Level.SEVERE, null, ex);
}
//oppure
try {
fis = new FileInputStream(file);
// Here BufferedInputStream is added for fast reading.
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
// dis.available() returns 0 if the file does not have more lines.
while (dis.available() != 0) {
// this statement reads the line from the file and print it to
// the console.
String line = dis.readLine();
if (line.substring(0,4).equalsIgnoreCase("path")) {
this.defaultPath = line.substring(7);
System.out.println("path = " + line.substring(7));
}
if (line.substring(0,2).equalsIgnoreCase("id")) {
this.id = line.substring(5);
System.out.println("id = " + line.substring(5));
}
if (line.substring(0,2).equalsIgnoreCase("pw")) {
this.pw = line.substring(5);
}
}
// dispose all the resources after using them.
fis.close();
bis.close();
dis.close();
} catch (FileNotFoundException e) {
//oppure
import java.io.*; class cat { public static void main (String args[]) { String thisLine; //Loop across the arguments for (int i=0; i < args.length; i++) { //Open the file for reading try { BufferedReader br = new BufferedReader(new FileReader(args[i])); while ((thisLine = br.readLine()) != null) { // while loop begins here System.out.println(thisLine); } // end while } // end try catch (IOException e) { System.err.println("Error: " + e); } } // end for } // end main }
//SCRIVE UN FILE
private FileWriter fw;
//Crea il file
try { this.fw = new FileWriter(path + "\\" + fileName + ".txt", true); } catch (IOException e) { ioData.sendMessage(e.getMessage()); e.printStackTrace();}
//Scrive su file
try { this.fw.write("[" + this.now() + "][DEBUG] " + stringa + "\n"); this.fw.flush(); } catch (IOException e) { ioData.sendMessage(e.getMessage()); e.printStackTrace();}
//Chiude il file
public void closeFile(){ try { this.fw.flush(); this.fw.close(); } catch (IOException e) { ioData.sendMessage(e.getMessage()); e.printStackTrace();} }