Visualizzazione dei risultati da 1 a 3 su 3
  1. #1
    Utente di HTML.it L'avatar di freetom
    Registrato dal
    Nov 2001
    Messaggi
    3,725

    Programmino analisi txt...lo vorrei realizzare anke in java :)

    Per adesso mi basterebbe sapere come realizzare un piccolo programmino JAVA che mi possa semlicemente... "leggere" riga per riga un file txt di tot righe...

    e che mi possa magari per adesso solo riportare ad es. la riga n.3 in un altro file txt denominato riga3.txt appunto

    GRAZIE a chi mi aiuta!

    By un neofita ma già appassionato javariano

  2. #2
    Utente di HTML.it
    Registrato dal
    Feb 2004
    Messaggi
    724
    è gia stato trattato più e più volte... basta cercare un po':


    Question
    How can I read from, and write to, files in Java?

    Answer
    Stand-alone applications written in Java can access the local file-system, but applets executing under the control of a browser cannot. With this in mind, lets take a look at two simple applications that write a line of text to a file, and read it back.

    codice:
    import java.io.*;
    
    public class MyFirstFileWritingApp
    {
    	// Main method
    	public static void main (String args[])
    	{
    		// Stream to write file
    		FileOutputStream fout;		
    
    		try
    		{
    		    // Open an output stream
    		    fout = new FileOutputStream ("myfile.txt");
    
    		    // Print a line of text
    		    new PrintStream(fout).println ("hello world!");
    
    		    // Close our output stream
    		    fout.close();		
    		}
    		// Catches any error conditions
    		catch (IOException e)
    		{
    			System.err.println ("Unable to write to file");
    			System.exit(-1);
    		}
    	}	
    }
    This simple application creates a FileOutputStream, and writes a line of text to the file. You'll notice that the file writing code is enclosed within a try { .... } catch block. If you're unfamiliar with exception handling in Java, this requires a little explanation.
    
    Certain methods in Java have the potential to throw an error condition, known as an exception. We have to trap these error conditions, so we 'catch' any exceptions that may be thrown.
    
    The task of reading from a file is also just as easy. We create a FileInputStream object, read the text, and display it to the user.
    
    import java.io.*;
    
    public class MyFirstFileReadingApp
    {
    	// Main method
    	public static void main (String args[])
    	{
    		// Stream to read file
    		FileInputStream fin;		
    
    		try
    		{
    		    // Open an input stream
    		    fin = new FileInputStream ("myfile.txt");
    
    		    // Read a line of text
    		    System.out.println( new DataInputStream(fin).readLine() );
    
    		    // Close our input stream
    		    fin.close();		
    		}
    		// Catches any error conditions
    		catch (IOException e)
    		{
    			System.err.println ("Unable to read from file");
    			System.exit(-1);
    		}
    	}	
    }

  3. #3
    Questo metodo però è deprecato !! si dovrebbe usare il BufferedReader!!!!

    Attenzione siete deprecati!!!!
    O ( ( |-| | O |)| |= /\ |_ ( o

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.