Hi,
I want to know what will i used to get a file(server.logs) that resides in the linux. I am using a windows OS. I dont know what to do. Thanks.
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.
Hi,
I want to know what will i used to get a file(server.logs) that resides in the linux. I am using a windows OS. I dont know what to do. Thanks.
Hello Truffy,
Do you know the location of the log files? What do you wish to do with the file once you have it?
Please use [highlight=Java] code [/highlight] tags when posting your code.
Forum Tip: Add to peoples reputation by clicking the button on their useful posts.
Hello,
The location of it is on the linux server. I am accessing it manually thru putty or through winscp. Once i have the file, i will read through it and parse the data that ive needed. e.i. the incoming request and the outgoing repuest. something like that.
Hello Truffy,
Here is a simple example. It will read in a file called log.txt and print it's content to the console:
You can change the location of the file by editing the path String and the name of the log file by editing the file String.import java.io.BufferedReader; import java.io.FileInputStream; import java.io.InputStreamReader; public class Truffy { /** * JavaProgrammingForums.com */ // Path to log file . = application root private static String path = "."; // Log file name private static String file = "log.txt"; public void getAndReadFile(){ try { FileInputStream in = new FileInputStream(path + "/" + file); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; // Read file line by line // Processing would be called here while((strLine = br.readLine())!= null) { System.out.println(strLine); } }catch(Exception e){ System.out.println(e); } } public static void main(String[] args) { Truffy t = new Truffy(); t.getAndReadFile(); } }
Please use [highlight=Java] code [/highlight] tags when posting your code.
Forum Tip: Add to peoples reputation by clicking the button on their useful posts.
hi, this is the result if i tried your code:
java.io.FileNotFoundException: \\10.123.456.78\apps\user\staging\M\log\success.lo g (The network path was not found)
here's the code.
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package aa; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.InputStreamReader; public class Truffy { /** * JavaProgrammingForums.com */ // Path to log file . = application root private static String path = "////10.123.456.78//apps//user//staging//M//log"; // Log file name private static String file = "success.log"; public void getAndReadFile(){ try { FileInputStream in = new FileInputStream(path + "/" + file); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; // Read file line by line // Processing would be called here while((strLine = br.readLine())!= null) { System.out.println(strLine); } }catch(Exception e){ System.out.println(e); } } public static void main(String[] args) { Truffy t = new Truffy(); t.getAndReadFile(); } }
Maybe that directory, or whole server requires password authentication?
so given that it need an authentication, how will the code will be? thank you.
Please use [highlight=Java] code [/highlight] tags when posting your code.
Forum Tip: Add to peoples reputation by clicking the button on their useful posts.
yeah. actually i get it using WinSCP3.
OK in that case your user has access to the path/file in question.
Are you running this application in a Linux environment?
If so, I'm thinking it could be something to do with the path String.
Why so many / at the beginning?private static String path = "////10.123.456.78//apps//user//staging//M//log";
If you are not running this from within Linux, you can't just run this in Windows and expect it to grab the file from a linux box. There needs to be some kind of authentication process.
Please use [highlight=Java] code [/highlight] tags when posting your code.
Forum Tip: Add to peoples reputation by clicking the button on their useful posts.
i am not running in linux. thats my problem, i need a program for the authnetication process.
Why not login to the Linux box and launch the application from there?
Please use [highlight=Java] code [/highlight] tags when posting your code.
Forum Tip: Add to peoples reputation by clicking the button on their useful posts.
we will be using it in the windows apps.
Hello Truffy,
You said you normally use WinSCP to connect to the linux box. Does that mean you can connect via FTP?
If so, you could try this code. You may need to edit it slightly.
import java.io.*; import java.net.URL; import java.net.URLConnection; public class JFTP { /** * JavaProgrammingForums.com */ public static String server = "10.123.456.78"; public static String userName = "username"; public static String password = "password"; public static String fileName = "apps/user/staging/M/log/success.log"; public static void main(String[] args) throws Exception{ System.out.println("Connecting to FTP server..."); //Connection String URL url = new URL("ftp://"+userName+":"+password+"@"+server+"/"+fileName+";type=i"); URLConnection con = url.openConnection(); //Add error handling here for non connections System.out.println("Connected."); BufferedInputStream in = new BufferedInputStream(con.getInputStream()); System.out.println("Downloading file."); //Downloads the selected file to the C drive FileOutputStream out = new FileOutputStream("C:\\success.log"); int i = 0; byte[] bytesIn = new byte[1024]; while ((i = in.read(bytesIn)) >= 0) { out.write(bytesIn, 0, i); } out.close(); in.close(); System.out.println("File downloaded to C drive"); } }
Please use [highlight=Java] code [/highlight] tags when posting your code.
Forum Tip: Add to peoples reputation by clicking the button on their useful posts.
hi,
thanks for the code. i will try it later.
give the feedback once done.
hi,
i already try the code. and here's the error:
init:
deps-jar:
compile-single:
run-single:
Connecting to FTP server...
Connected.
Exception in thread "main" java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl .java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSoc ketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.j ava:182)
at java.net.Socket.connect(Socket.java:520)
at java.net.Socket.connect(Socket.java:470)
at sun.net.NetworkClient.doConnect(NetworkClient.java :157)
at sun.net.NetworkClient.openServer(NetworkClient.jav a:118)
at sun.net.ftp.FtpClient.openServer(FtpClient.java:48 8)
at sun.net.ftp.FtpClient.openServer(FtpClient.java:47 5)
at sun.net.http://www.protocol.ftp.FtpURLConnec...ction.java:270)
at sun.net.http://www.protocol.ftp.FtpURLConnec...ction.java:352)
at aa.JFTP.main(JFTP.java:29)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)
i dont know why it is not working.
Last edited by Truffy; June 7th, 2009 at 09:46 PM. Reason: it wokrs in another server
hi, i ty using another server and it is working. maybe the server that i am accessing doesnt have an ftp grant. anyway, the next part of it is since it is a server logs, if someone transact on it, my program have to know it. how can i add a listen to it? is there any refresh commands to it? or compare commands?
Good morning Truffy,
I'm glad that code is working with the new server.
So you want to watch the log file and when it is changed, your application does something?Originally Posted by Truffy
How about this code. It will watch the log file every 5 seconds and if it changes size, the doSomething() method is called.
import java.io.*; public class TruffyLog { /** * JavaProgrammingForums.com */ public static long StartSize; public long getFileSize(String filename) { File file = new File(filename); if (!file.exists() || !file.isFile()) { System.out.println("File doesn\'t exist"); return -1; } return file.length(); } public static void doSomething(){ System.out.println("Log file has been changed"); } public static void main(String[] args) throws Exception{ System.out.println("Watching file"); TruffyLog tl = new TruffyLog(); long size = tl.getFileSize("success.log"); StartSize = size; System.out.println("Start size: " + StartSize); while(true){ size = tl.getFileSize("success.log"); System.out.println("Filesize in bytes: " + size + " - No change"); //If file size changes... if(size < StartSize | size > StartSize){ doSomething(); StartSize = size; } // Sleep 5 seconds Thread.sleep(5000); } } }
Please use [highlight=Java] code [/highlight] tags when posting your code.
Forum Tip: Add to peoples reputation by clicking the button on their useful posts.
yup i will watch it. then if it the size change, i will fecth the information log on it and show it. thanks. i will try your code. will give feedback after done.
Additional problem occured:
import java.io.*; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.logging.Level; import java.util.logging.Logger; public class TruffyLog { /** * JavaProgrammingForums.com */ public static long StartSize; public long getFileSize(String filename) { File file = new File(filename); if (!file.exists() || !file.isFile()) { System.out.println("File doesn\'t exist"); return -1; } return file.length(); } public static void doSomething(){ System.out.println("Log file has been changed"); ex tryME = new ex(); tryME.createAndShowGUI(); } public static void main(String[] args) throws Exception{ System.out.println("Watching file"); TruffyLog tl = new TruffyLog(); long size = tl.getFileSize("c:\\server.log"); StartSize = size; System.out.println("Start size: " + StartSize); while(true){ size = tl.getFileSize("c:\\server.log"); System.out.println("Filesize in bytes: " + size + " - No change"); //If file size changes... if(size < StartSize | size > StartSize){ doSomething(); StartSize = size; } // Sleep 5 seconds Thread.sleep(5000); } } }
heres the problem: whenever the file changed. another GUI shows and if the file changed for ten times, there will be ten GUI. and i only want 1 GUI. and it will only refresh the present GUI.import javax.swing.JFrame; import javax.swing.*; public class ex { public static void createAndShowGUI(){ JFrame frame = new JFrame("Hello Java Forums!!!"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel label = new JLabel("Hello Java forums"); frame.add(label); frame.setSize(300,200); frame.setVisible(true); } public static void main(String[] args){ javax.swing.SwingUtilities.invokeLater(new Runnable(){ public void run(){createAndShowGUI();} }); }; }
how can i do that?
pls help.
thanks.
I haven't tested that code on large files but as far as im aware, it will download them as fast as your internet connection will allow. I'm not sure how you would make the process faster.Originally Posted by Truffy
Please use [highlight=Java] code [/highlight] tags when posting your code.
Forum Tip: Add to peoples reputation by clicking the button on their useful posts.
Try this. I have used a boolean variable which is set to false at run time. When the log file is changed for the first time, the createAndShowGUI method is run and the boolean variable is set to true. When the log file changes again, the if statement checks to see if the boolean variable is true. If it is true then the program knows to run the refreshGUI method instead of the createAndShowGUI method.Originally Posted by Truffy
import java.io.*; public class TruffyLog { /** * JavaProgrammingForums.com */ public static long StartSize; private static boolean on = false; public long getFileSize(String filename) { File file = new File(filename); if (!file.exists() || !file.isFile()) { System.out.println("File doesn\'t exist"); return -1; } return file.length(); } public static void doSomething(){ System.out.println("Log file has been changed"); ex tryME = new ex(); if(on == false){ System.out.println("NEW GUI"); tryME.createAndShowGUI(); on = true; } else if(on == true){ System.out.println("REFRESH GUI"); tryME.refreshGUI(); } } public static void main(String[] args) throws Exception{ System.out.println("Watching file"); TruffyLog tl = new TruffyLog(); long size = tl.getFileSize("C:\\success.log"); StartSize = size; System.out.println("Start size: " + StartSize); while(true){ size = tl.getFileSize("C:\\success.log"); System.out.println("Filesize in bytes: " + size + " - No change"); //If file size changes... if(size < StartSize | size > StartSize){ doSomething(); StartSize = size; } // Sleep 5 seconds Thread.sleep(5000); } } }import javax.swing.JFrame; import javax.swing.*; public class ex { public static JFrame frame = new JFrame("Hello Java Forums!!!"); public static JLabel label; public static void createAndShowGUI() { frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); label = new JLabel("Hello Java forums"); frame.add(label); frame.setSize(300, 200); frame.setVisible(true); } public static void refreshGUI(){ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.remove(label); JLabel label2 = new JLabel("JAVA PROGRAMMING FORUMS!"); frame.add(label2); frame.setSize(300, 200); frame.setVisible(true); } /* public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); }; */ }
Please use [highlight=Java] code [/highlight] tags when posting your code.
Forum Tip: Add to peoples reputation by clicking the button on their useful posts.
Truffy (June 10th, 2009)
thanks very much. i will not yet press the solved because i have many interfaces in this program and i appreciate much of your help. thanks very much. ill post again soon the next issues i will encounter.