Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Reading a txt file

Options
  • 15-03-2010 10:19pm
    #1
    Registered Users Posts: 3,624 ✭✭✭


    ok im using eclipse
    Can i create an array so that i can either choose to show particular files or delete certain files..

    i was also wondering what is the code to delete and amend a txt file
    public class apples {
    	public static void main(String[] args){
    		createfile g = new createfile();
    		g.openFile();
    		g.addRecords();
    		g.closeFile();
    		
    
    		
    		readfile r = new readfile();
    		r.openFile();
    		r.readFile();
    		r.closeFile();
    		
    		
    	}
    
    }
    
    import java.io.*;
    import java.lang.*;
    import java.util.*;
    
    public class createfile {
    	private Formatter x;
    	public void openFile(){
    		try{
    			x = new Formatter("chinese.txt");
    		}
    		catch(Exception e){
    			System.out.println("you have an error");
    			
    		}
    	}
    	
    	public void addRecords(){
    		x.format("%s%s%s%n","20 ","bucky ","roberts ");
    		
    		
    		x.format("%s%s%s%n","20 ","hhhhh ","rgggggg ");
    		
    		x.format("%s%s%s%n","20 ","hhhhh ","rgggggg ");
    		x.format("%s%s%s%n","20 ","hhhhh ","rgggggg ");
    		
    		
    	}
    	
    	public void closeFile(){
    		x.close();
    	}
    
    }
    
    import java.io.*;
    import java.util.*;
    public class readfile {
    	
    	private Scanner x;
    	
    	public void openFile(){
    		try{
    			x = new Scanner(new File("chinese.txt"));
    		}
    		catch(Exception e){
    			System.out.print("could not find file");
    		}
    	}
    	
    	public void readFile(){
    		while(x.hasNext()){
    			String a = x.next();
    			String b = x.next();
    			String c = x.next();
    			
    			System.out.printf("%s%s%s%n" a,b,c);
    			
    		}
    	}
    	
    	public void closeFile(){
    		x.close();
    	}
    
    
    }
    

    Im trying this guys tutorial
    http://www.youtube.com/user/thenewboston#p/search/0/3RNYUKxAgmw

    I was also wondering using
    public void readFile(){
    		while(x.hasNext()){
    			String a = x.next();
    			String b = x.next();
    			String c = x.next();
    			
    			System.out.println("%s%s%s%n" ,a,b,c);
    			
    		}
    
    Can i create an array so that i can either choose to show particular files or delete certain files..

    i was also wondering what is the code to delete and amend a txt file




    I was also wondering can i keep updating this thread with other problems im having when trying these tutorials


Comments

  • Registered Users Posts: 1,109 ✭✭✭Skrynesaver


    The error you are seeing was due to the multiple paramaters supplied to the println method if you concatonate your strings in the prinln call these errors will go away.

    Hope that helps


Advertisement