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 22 of 22

Thread: what is the right class to write into text file in a specific line (the end of the text file) ?

  1. #1
    Member
    Join Date
    Sep 2019
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default what is the right class to write into text file in a specific line (the end of the text file) ?

    im asking because my code do override.
    i tried use write/append i know there BufferedWriter maybe with this ?
    even thought maybe do reader to check if the line empty and after that do write (but i dont know how change lines in write so....).

    before answer check the mac and macEnter variables. the mac Enter its the mac with "/"n ( ithink maybe its will help for the override but its not )

    String banlist = "ftp://username:pass@host/ss/banlist.txt", mac = null, command = scanner.nextLine(), macEnter = null, line = null;
    				boolean targetIsAdmin = false, targetInBan = false; 
     
    				while(!command.equals("!exit")) {
     
    					if(command.equals("!ban")) {
    						System.out.println("type the target mac");
    						mac = scanner.nextLine();
    						targetIsAdmin = searchTxtFTP(adminlist, mac);
    						targetInBan = searchTxtFTP(banlist, mac);
    						if(!targetIsAdmin && !targetInBan) {
    							try {
     
    								URL url = new URL(banlist);
    								URLConnection con = url.openConnection();
    								OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream());
    							    out.write(macEnter);
    								out.close();
    								System.out.println("NOTICE: you have added the client to the banlist");
    							} catch(Exception e) {
    								e.printStackTrace();
    							}
    						} else if(targetIsAdmin && !targetInBan) System.out.println("EROR: you can't give ban to admin!");
    						else System.out.println("EROR: the target:" + " " + serial + "is already on ban list");
    					}
     
    					System.out.println("commands:");
    					System.out.println("!ban, !exit");
    					command = scanner.nextLine();
    				}
     
    				scanner.close();
    				System.exit(1);
     
    		} catch(Exception e) {
    			e.printStackTrace();
    		}
     
    	}
     
    	public static boolean searchTxtFTP(String userPassHostDirectoryTxtfile, String stringTxt) {
     
    		boolean found = false;
    		String line = null;
     
    		try {
     
    			URL url = new URL(userPassHostDirectoryTxtfile);
    			URLConnection con = url.openConnection();
    			BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
     
    			while ((line = in.readLine()) != null) {
     
    				if(line.contains(stringTxt)) {
    					found = true;
    					break;
    				}
     
    			}
     
    			in.close();
    		} catch(Exception e) {
    			e.printStackTrace();
    		}
    		return found;
     
    	}
    Last edited by doronbachar; October 31st, 2019 at 05:26 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: what is the right class to write into text file in a specific line (the end of the text file) ?

    Some of the output classes have a special boolean parameter in their constructor for appending to a file.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Sep 2019
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: what is the right class to write into text file in a specific line (the end of the text file) ?

    Quote Originally Posted by Norm View Post
    Some of the output classes have a special boolean parameter in their constructor for appending to a file.
    Can you give me one that its constructor gets OutputStreamWriter and have this appending ?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: what is the right class to write into text file in a specific line (the end of the text file) ?

    I don't know which one it is. You have to Look at the API doc for the output files:
    http://docs.oracle.com/javase/8/docs/api/index.html
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Sep 2019
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: what is the right class to write into text file in a specific line (the end of the text file) ?

    Quote Originally Posted by Norm View Post
    I don't know which one it is. You have to Look at the API doc for the output files:
    http://docs.oracle.com/javase/8/docs/api/index.html
    sorry, I havent been at the computer lately.
    i tried some IO with append function not work.

    this is the permission of the file text on the FTP its ok ?
    	rw-r--r--

    i try also with FTPClient appendFile and appendFileStream not work either.
    i saw somthing maybe will helps (but its for local text fil)
    FileWriter fw = new FileWriter(filename,true);

    can you help me ?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: what is the right class to write into text file in a specific line (the end of the text file) ?

    Yes, that looks like the constructor that can be used when you want to append to a file instead of rewriting it.

    This has nothing to do with FTP commands. This is for local files.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Sep 2019
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: what is the right class to write into text file in a specific line (the end of the text file) ?

    Quote Originally Posted by Norm View Post
    Yes, that looks like the constructor that can be used when you want to append to a file instead of rewriting it.

    This has nothing to do with FTP commands. This is for local files.
    so, do you have idea for my problem ?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: what is the right class to write into text file in a specific line (the end of the text file) ?

    have idea for my problem ?
    Is your problem with using FTP?

    I don't know much about FTP.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Sep 2019
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: what is the right class to write into text file in a specific line (the end of the text file) ?

    Quote Originally Posted by Norm View Post
    Is your problem with using FTP?

    I don't know much about FTP.
    thank for helping so far.
    i have question about exec (probably simple one):
    Runtime runtime = Runtime.getRuntime();
    					try {
    						runtime.exec("C:/Program Files (x86)/qBittorrent/qBittorrent.exe");
    						System.out.println("Dear Admin: we have opened another window for you for admin commands");
    					} catch(Exception e) {
    						e.printStackTrace();
    					}

    when i put software (that im not write) its work and call the program.
    but, if i try on somthing that i write (on java) it dosent, why ?
    runtime.exec("C:/Program Files (x86)/qBittorrent/a.exe"); 
    //a.exe its jar converted by Launch4j (work if i click the program 100%).

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: what is the right class to write into text file in a specific line (the end of the text file) ?

    , if i try on somthing that i write (on java) it dosent
    Please post the complete code that can be compiled and executed for testing.

    Make sure your code reads and prints the streams returned by the process - normal and error.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Sep 2019
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: what is the right class to write into text file in a specific line (the end of the text file) ?

    Quote Originally Posted by Norm View Post
    Please post the complete code that can be compiled and executed for testing.

    Make sure your code reads and prints the streams returned by the process - normal and error.
    String adminlist = "ftp://user:pass@host/ss/adminlist.txt";
    				if(searchTxtFTP(adminlist, serial)) {
    					Runtime runtime = Runtime.getRuntime();
    					try {
    						runtime.exec("C:/a.exe");
    						System.out.println("Dear Admin: we have opened another window for you for admin commands");
    					} catch(Exception e) {
    						e.printStackTrace();
    					}	
    				}
    				//end #3
     
     
    //#searchTxtFTP associated #1 + #2
    	public static boolean searchTxtFTP(String userPassHostDirectoryTxtfile, String stringTxt) {
     
    		boolean found = false;
    		String line = null;
     
    		try {
     
    			URL url = new URL(userPassHostDirectoryTxtfile);
    			URLConnection con = url.openConnection();
    			BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
     
    			while ((line = in.readLine()) != null) {
     
    				if(line.contains(stringTxt)) {
    					found = true;
    					break;
    				}
    			}
     
    			in.close();
    		} catch(Exception e) {
    			e.printStackTrace();
    		}
    		return found;
     
    	}//end #searchTxtFTP

    no errors: and even this line printed: Dear Admin: we have opened another window for you for admin commands
    Client. -=[created by doronbachar]=-
     
    [disk mac found]: 0025_385B_71B1_26B3
    Dear Admin: we have opened another window for you for admin commands
    screenshot has been taken
    your folder is located
    the image is uploaded successfully
    The file was successfully deleted from your computer

    a.exe
    also work 100% separately

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: what is the right class to write into text file in a specific line (the end of the text file) ?

    The Runtime exec method returns a Process object that has streams the show the normal and error output from the process.
    You need to get references to those streams to read from and print out what they contain so you can see if there are any errors.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Sep 2019
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: what is the right class to write into text file in a specific line (the end of the text file) ?

    Quote Originally Posted by Norm View Post
    The Runtime exec method returns a Process object that has streams the show the normal and error output from the process.
    You need to get references to those streams to read from and print out what they contain so you can see if there are any errors.
    like that ?
    Process process = runtime.exec("C:/a.exe");
    System.out.println(process);

    if yes its return:
    java.lang.ProcessImpl@1b6d3586

    --- Update ---

    i also now check in the task mangaer i saw a.exe its work when i run the program, but i cant see its opened why ?

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: what is the right class to write into text file in a specific line (the end of the text file) ?

    That is not what I was talking about.
    Read the API doc for the Process class to find what methods to call to get the streams that the process writes to. Your code must read and print what those streams contain so you can see if there were any errors.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Sep 2019
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: what is the right class to write into text file in a specific line (the end of the text file) ?

    Quote Originally Posted by Norm View Post
    That is not what I was talking about.
    Read the API doc for the Process class to find what methods to call to get the streams that the process writes to. Your code must read and print what those streams contain so you can see if there were any errors.
    Oh, stream got it, when i be near to my pc, i will send to you

  16. #16
    Member
    Join Date
    Sep 2019
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: what is the right class to write into text file in a specific line (the end of the text file) ?

    Quote Originally Posted by Norm View Post
    That is not what I was talking about.
    Read the API doc for the Process class to find what methods to call to get the streams that the process writes to. Your code must read and print what those streams contain so you can see if there were any errors.
    i did this code, i saw after i run it, its start the main program (like like he should) when eh get to this code her.
    in task manager a.exe work, and the console of the main program print 2 lines from a.exe (lol thats not opended in another window its stack also the main program because of that).

    i think maybe 2 things:
    1. exec needed a swing program not console on.
    or and
    2. needed do thread stuff.

    you have another idea ? i will try now to do the thread stuff first and update.

    Runtime runtime = Runtime.getRuntime();
    					try {
     
    						String line = null;
    						Process process = runtime.exec("C:/a.exe");
    						BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
     
    						while ((line = in.readLine()) != null) System.out.println(line);
     
    						in.close();
    						System.out.println("Dear Admin: we have opened another window for you for admin commands");
     
    					} catch(Exception e) {
    						e.printStackTrace();
    					}


    --- Update ---

    the Thread stuff do more mess of mixing.

    i will try now simple swing

    --- Update ---

    Norm: i check now with simple swing program and now its work.
    if i do console its blend with the main program console.
    so for now its will work probably :1. new Thread + 2. swing
    maybe i will try do exec to cmd (and with cmd commands open the second program).
    i will update

    --- Update ---

    ok after i tried i thnk its needed to be swing program (its work even without Thread, but without thread in main program wait until the program load.
    there way to do with un swing code ?
    Last edited by doronbachar; October 30th, 2019 at 01:09 AM.

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: what is the right class to write into text file in a specific line (the end of the text file) ?

    Sorry, I do not understand your question.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Member
    Join Date
    Sep 2019
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: what is the right class to write into text file in a specific line (the end of the text file) ?

    Quote Originally Posted by Norm View Post
    Sorry, I do not understand your question.
    when i do the program without swing, its mixed the main program and the program that called from exec together.
    i try to do simple swing (jbutton) just for trying and main program and the program that called from exec separately as they should be.

    so im asking if you know way to avoid swing ? (i dont know swing) that main program and the program that called from exec separately.
    if you dont know away i let the client to do manually click (for open th another program)

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: what is the right class to write into text file in a specific line (the end of the text file) ?

    way to avoid swing ?
    Do you mean a GUI when you say swing?
    Are you trying to write a program that reads/writes in a command prompt instead of using a GUI?

    For a java program to use a command prompt, the user must open the command prompt first and then execute the java program there.
    If you don't understand my answer, don't ignore it, ask a question.

  20. #20
    Member
    Join Date
    Sep 2019
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: what is the right class to write into text file in a specific line (the end of the text file) ?

    Quote Originally Posted by Norm View Post
    Do you mean a GUI when you say swing?
    Are you trying to write a program that reads/writes in a command prompt instead of using a GUI?

    For a java program to use a command prompt, the user must open the command prompt first and then execute the java program there.
    yes i meant i never used GUI (haha i learn java a week or 2 ago, dont really care about this GUI) i just write a code and when im done i use blue4j to convert to exe (from jar).

    but i try exec to java GUI program and its open, when the program not usu GUI (even its exe) java mix the main program and the program from exec (that not using GUI in exe).

  21. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: what is the right class to write into text file in a specific line (the end of the text file) ?

    If the java program uses GUI then it will open a window for input however it is executed allowing user interaction.
    If the java program only uses the console, then the user must open a comand prompt window to enter data into the program and see what it prints out.
    On Windows, using a batch file (.bat) to execute the java program will open a command prompt window allowing user interaction.

    Another option would be to use the JOPtionPane class. I will present a small window and get input without a swing GUI.
    If you don't understand my answer, don't ignore it, ask a question.

  22. #22
    Member
    Join Date
    Sep 2019
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: what is the right class to write into text file in a specific line (the end of the text file) ?

    Quote Originally Posted by Norm View Post
    If the java program uses GUI then it will open a window for input however it is executed allowing user interaction.
    If the java program only uses the console, then the user must open a comand prompt window to enter data into the program and see what it prints out.
    On Windows, using a batch file (.bat) to execute the java program will open a command prompt window allowing user interaction.

    Another option would be to use the JOPtionPane class. I will present a small window and get input without a swing GUI.
    ok

    about my original question how add text in remote text on FTP:
    after struggles its work now, maybe its will help others(i learn simple regex for this hh)
    and read beforce i write (for saving manually the old text, append fuction not working well i try so so many... so its do it yourself kind a thing):
    URL urlR = new URL(banlist);
                        URLConnection conR = urlR.openConnection();
                        BufferedReader inR = new BufferedReader(new InputStreamReader(conR.getInputStream()));
     
                        String copyBanList = ""; //empty string
     
     
                        while ( (line = inR.readLine()) != null ) copyBanList += "\n" +  line.trim(); //after read line(with trim), do new line
     
                        inR.close();
     
                        if(!targetIsAdmin && !targetInBan) {
                            URL urlW = new URL(banlist);
                            URLConnection conW = urlW.openConnection();
                            BufferedWriter outW = new BufferedWriter(new OutputStreamWriter(conW.getOutputStream()));    
     
                            //add into copyBanList new line if i'ts not the first one
                            outW.write((copyBanList + "\n" +  mac.replaceAll("\n+", "\n")).replaceAll("^\n+", "")); 
                            outW.flush();
                            outW.close();
                            System.out.println("NOTICE: you have added the client" + " " + mac + " " +  "to the banlist");
                        } else if(targetIsAdmin && !targetInBan) System.out.println("EROR: you can't give ban to admin!");
                        else System.out.println("EROR: the target:" + " " + mac + " " +  "is already on ban list");

Similar Threads

  1. Replies: 7
    Last Post: March 10th, 2014, 04:28 PM
  2. Editing a Text File in Java, a specific part.
    By sakonpure6 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 22nd, 2013, 03:32 PM
  3. Java code Split text file into multiple text file
    By jayraj in forum What's Wrong With My Code?
    Replies: 26
    Last Post: April 19th, 2013, 06:14 AM
  4. Writing to a specific line in a text file
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 7th, 2011, 09:11 PM
  5. java program to copy a text file to onother text file
    By francoc in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 23rd, 2010, 03:10 PM