Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Reading a txt file

  • 15-03-2010 10:19PM
    #1
    Registered Users, Registered Users 2 Posts: 3,635 ✭✭✭


    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, Registered Users 2 Posts: 1,110 ✭✭✭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