Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: TxT file - write in the next line

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Location
    Lithuania
    Posts
    23
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Lightbulb TxT file - write in the next line

    Hello guys I need a little help(I think). So i start work with TxT files. And i need know how to write text in the next line if file exist. My code just last text replace new.. Here code :
    Main :
     package YoYo;
    import java.util.Scanner;
     
    public class Main {
    	static String atsakymas;
    	static String FileName;
     
     
     
    	static Scanner kl = new Scanner(System.in);
    	static Scanner kl2 = new Scanner(System.in);
     
    	static Creating create = new Creating();
    	public static void main(String[]args) {
     
    		String irasyti = grazina();
     
    		System.out.println("Please enter file name");
    		FileName = kl.nextLine();
    		create.createFile(FileName,irasyti);
    		create.closeFile();
     
     
     
    		}
     
    	static String grazina() {
    		String irasyti = null;
    		System.out.println("Are you want write something in file?");
    		atsakymas = kl2.nextLine();
    		if (atsakymas.equals("Yes")) {
    			System.out.println("What you want write?");
    			irasyti = kl.nextLine();
    			System.out.println(irasyti);
     
    		}
    		return irasyti;
     
    	}
     
    }

    Creating:
     
    package YoYo;
     
    import java.io.FileNotFoundException;
    import java.io.PrintWriter;
    import java.io.UnsupportedEncodingException;
    import java.util.Formatter;
     
    public class Creating {
     
    	PrintWriter x;
    	String file;
    	String WhatWrite;
     
    	public void createFile(String filename, String writing) {
    		file = filename;
    		WhatWrite = writing;
    		try {
     
    			x = new PrintWriter(file);
    			x.println(WhatWrite);
     
     
    		} catch(Exception e) {
    			System.out.println("Error");
    		}
     
    	}
     
     
     
    	public void closeFile() {
    		x.close();
    	}
     
     
     
     
     
     
    }

    Sorry for bad English


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: TxT file - write in the next line

    Quote Originally Posted by xkendzu View Post
    i need know how to write text in the next line if file exist.
    			x = new PrintWriter(file);
    If you want to "append" to the content of an already existing file:

    new PrintWriter(new FileOutputStream(file, true));

    In FileOutputStream constructor, "true" means "open in append mode".
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  3. The Following User Says Thank You to andbin For This Useful Post:

    xkendzu (March 5th, 2014)

  4. #3
    Junior Member
    Join Date
    Jan 2014
    Location
    Lithuania
    Posts
    23
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: TxT file - write in the next line

    Very very thank you andbin

Similar Threads

  1. read xml files and write to the txt file
    By RMAL in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: July 24th, 2018, 01:52 PM
  2. (Java) Overwrite line in .txt file
    By CruelCoin in forum Java Theory & Questions
    Replies: 4
    Last Post: December 18th, 2013, 10:40 AM
  3. Replies: 0
    Last Post: November 13th, 2012, 07:02 AM
  4. Program to read .TXT file one line at a time
    By JustForLawls in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 27th, 2012, 02:02 PM
  5. Write something to the next available line in a file
    By frozen java in forum Java Theory & Questions
    Replies: 2
    Last Post: December 7th, 2011, 02:09 AM