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.

Page 2 of 2 FirstFirst 12
Results 26 to 37 of 37

Thread: getting files in the linux server

  1. #26
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: getting files in the linux server

    Quote Originally Posted by Truffy
    how about the java ssh? do you know it?
    I don't have any SSH code examples. I suggest you look for a Java SSH API.

    SourceForge.net: SSHTools

    SourceForge.net: SSHTools: Files
    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.

  2. The Following User Says Thank You to JavaPF For This Useful Post:

    Truffy (June 10th, 2009)

  3. #27
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Thumbs up Re: getting files in the linux server

    Quote Originally Posted by Truffy View Post
    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.
    No problem Truffy. I'm glad I can help.
    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.

  4. #28
    Member Truffy's Avatar
    Join Date
    May 2009
    Location
    Philippines
    Posts
    93
    Thanks
    2
    Thanked 9 Times in 7 Posts

    Default Re: getting files in the linux server

    do you know about RMI? i really looking for the alternatives regarding my code. because it is so slow. reading files of 50MB size really makes it slow. I think if it tail it(hope there is a function for that) which means of knowing where in the file(line of file) it stops reading and when the file changes, the additional is the only one reads. do you kniow how will i code it?
    Last edited by Truffy; June 10th, 2009 at 09:17 PM.

  5. #29
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: getting files in the linux server

    Trail: RMI (The Java™ Tutorials)

    Are you looking to make the program print out only the changes in the file? You are still going to need to download and parse the entire log file atleast once.
    I'm not sure how to speed this up!
    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.

  6. #30
    Member Truffy's Avatar
    Join Date
    May 2009
    Location
    Philippines
    Posts
    93
    Thanks
    2
    Thanked 9 Times in 7 Posts

    Default Re: getting files in the linux server

    Quote Originally Posted by JavaPF View Post
    Trail: RMI (The Java™ Tutorials)

    Are you looking to make the program print out only the changes in the file? You are still going to need to download and parse the entire log file atleast once.
    I'm not sure how to speed this up!

    yup i need to printup the entire log and the next steps is to print only the changes on the file. but still it shows the other read data. which it will only append the changes in the file.

  7. #31
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: getting files in the linux server

    If I uderstand your probelm, then you have no option but to download the entire file the first time. Once you have done that, you can count the number of characters within that file...I don't care how then perhaps you could use the following code as an example so you only have to download new snippets

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
     
    public class Skipper {
     
    	public Skipper(){
    		BufferedReader br = new BufferedReader(
    				new InputStreamReader(System.in));
    		try {
    			br.skip(10);
    			System.out.println(br.readLine());
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
     
    	}
    	public static void main(String[] args){
    		new Skipper();
    	}
    }

    Regards,
    Chris

  8. #32
    Member Truffy's Avatar
    Join Date
    May 2009
    Location
    Philippines
    Posts
    93
    Thanks
    2
    Thanked 9 Times in 7 Posts

    Default Re: getting files in the linux server

    Quote Originally Posted by Freaky Chris View Post
    If I uderstand your probelm, then you have no option but to download the entire file the first time. Once you have done that, you can count the number of characters within that file...I don't care how then perhaps you could use the following code as an example so you only have to download new snippets

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
     
    public class Skipper {
     
    	public Skipper(){
    		BufferedReader br = new BufferedReader(
    				new InputStreamReader(System.in));
    		try {
    			br.skip(10);
    			System.out.println(br.readLine());
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
     
    	}
    	public static void main(String[] args){
    		new Skipper();
    	}
    }

    Regards,
    Chris
    Hi Cris,

    Thanks for it. I'll try this later.

    I'll keep you updated with the result.

    Cheers,
    Truffy

  9. #33
    Member Truffy's Avatar
    Join Date
    May 2009
    Location
    Philippines
    Posts
    93
    Thanks
    2
    Thanked 9 Times in 7 Posts

    Default Re: getting files in the linux server

    Hi Guys,

    Gotta a problem using skipper,

    Actually, i creat my own 1 but i dont know why it is not wotrking.

    Here's the code:



    import aa.GW.ReadFile;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.table.*;
     
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
     
    public class ex extends JPanel {
     
        JTextArea output;
        JList list = new JList();
        JList list2;
        JTable table  ;
        String newline = "\n";
        ListSelectionModel listSelectionModel;
        ListSelectionModel listSelectionModel2;
        JScrollPane listPane;
        JScrollPane listPane2;
        DefaultListModel listModel = new DefaultListModel();
        DefaultListModel listModel2 = new DefaultListModel();
        ListModel bigData;
        String out3 = "";// = new String[1000];
        int aa = 0;
        int index = 0;
        int index2 = 0;
        String[] lna;
        String out1 = " ";
        String out2 = " ";
        String out = " ";
        String out4 = " ";
        String out5 = " ";    
        String out6 = " ";   
        private int count;
        public static JFrame frame = new JFrame("GW");
        private static boolean on = TruffyLog.isOn();
     
     
        public ex() {
            super(new BorderLayout());
            System.out.println(on);
            list = new JList(listModel);
            index = list.getSelectedIndex(); //get selected index
            listSelectionModel = list.getSelectionModel();
            listSelectionModel.addListSelectionListener(
                              new SharedListSelectionHandler());
            listPane = new JScrollPane(list,
                                       ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
                                       ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
     
            list2 = new JList(listModel2);
            list2.repaint();
            list2.validate();
     
            //Build output area.
            output = new JTextArea(1, 10);
            output.setEditable(false);
            JScrollPane outputPane = new JScrollPane(output,
                                                     ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
                                                     ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
     
     
            //Do the layout.
            JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
            add(splitPane, BorderLayout.CENTER);
     
            JPanel topHalf = new JPanel();
            topHalf.setLayout(new BoxLayout(topHalf, BoxLayout.LINE_AXIS));
            JPanel listContainer = new JPanel(new GridLayout(1, 1));
            listContainer.setBorder(BorderFactory.createTitledBorder(
                    "GW"));
            listContainer.add(listPane);
     
            topHalf.setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 5));
    //        topHalf.add(listContainer);
            topHalf.add(listPane);
    //        topHalf.add(tableContainer);
     
            topHalf.setMinimumSize(new Dimension(100, 50));
            topHalf.setPreferredSize(new Dimension(100, 110));
            splitPane.add(topHalf);
     
            JPanel bottomHalf = new JPanel(new BorderLayout());
            bottomHalf.repaint();
            bottomHalf.validate();
            bottomHalf.add(outputPane, BorderLayout.CENTER);
            //XXX: next line needed if bottomHalf is a scroll pane:
            //bottomHalf.setMinimumSize(new Dimension(400, 50));
            bottomHalf.setPreferredSize(new Dimension(450, 135));
            splitPane.add(bottomHalf);
               bottomHalf.repaint();
            bottomHalf.validate();
     
          try {
            // Create and start the thread
            Thread thread = new ReadFile();
             thread.start();
                ReadFile rf = new ReadFile("c:\\a.txt");
                ArrayList a = rf.read();
                if(on == false){
             System.out.println("new read");
                  if (a.size() > 0) {
     
                    int i;
                    for (i = 0; i < a.size(); i++) {
     
                        if (a.contains("========================= INCOMING MESSAGE =========================")) {
                            String test = (String) a.get(i + 1);
                            int idx = 0;
                            String words[] = new String[1000];
                            int tokenCount;
                            StringTokenizer st = new StringTokenizer(test);
                            tokenCount = st.countTokens();
     
                            while (st.hasMoreTokens())
                            {
                                words[idx] = st.nextToken();
                                idx++;
                            }
                            for (idx = 0; idx < tokenCount; idx++) {
     
     
                                    out1 = words[idx]; 
                                    listModel.addElement(out1);
     
                            }
                        }
                    }
                }
                }
                else if(on == true){
                 System.out.println("get count ======================================================================================");
    //             a.size(getCount());
                 int x = getCount();
                  if (a.size() > 0) {
     
                    int i;
                    for (i = x; i < a.size(); i++) {
     
                        if (a.contains("========================= INCOMING MESSAGE =========================")) {
                            String test = (String) a.get(i + 1);
                            int idx = 0;
                            String words[] = new String[1000];
                            int tokenCount;
                            StringTokenizer st = new StringTokenizer(test);
                            tokenCount = st.countTokens();
     
                            while (st.hasMoreTokens()) 
                            {
                                words[idx] = st.nextToken();
     
     
                                    idx++;
                            }
                            out1 = words[idx]; 
                                    listModel.addElement(out1);
                                    listModel2.addElement(out1);
     
                        }
                    }
                }
     
     
                }
     
     
     
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }  
     
               list = new JList(listModel);
               index = list.getSelectedIndex(); //get selected index
               listSelectionModel = list.getSelectionModel();
               listSelectionModel.addListSelectionListener(
               new SharedListSelectionHandler());
               listPane = new JScrollPane(list,
               ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
               ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
     
               list2 = new JList(listModel2);
               list2.repaint();
               list2.validate();
           }
     
     
     
        public static void createAndShowGUI() {
            //Create and set up the window.
     
            frame.dispose();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            //Create and set up the content pane.
            ex demo = new ex();
            demo.setOpaque(true);
            frame.setContentPane(demo);
     
            //Display the window.
            frame.pack();
            frame.setVisible(true);
        }
     
          public static void refreshGUI(){
     
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            //Create and set up the content pane.
            ex demo = new ex();
            demo.setOpaque(true);
            frame.setContentPane(demo);
     
            //Display the window.
            frame.pack();
            frame.setVisible(true);
     
        }
     
    //    public static void main(String[] args) {
    //        //Schedule a job for the event-dispatching thread:
    //        //creating and showing this application's GUI.
    //        javax.swing.SwingUtilities.invokeLater(new Runnable() {
    //
    //            public void run() {
    //                createAndShowGUI();
    //            }
    //        });
    //    }
     
        public String getOut3() {
            return out3;
        }
     
        public void setOut3(String out3) {
            this.out3 = out3;
        }
     
        public int getCount() {
            return count;
        }
     
        public void setCount(int count) {
            this.count = count;
        }
     
        class SharedListSelectionHandler implements ListSelectionListener {
     
            public void valueChanged(ListSelectionEvent e) {
                ListSelectionModel lsm = (ListSelectionModel) e.getSource();
     
                    // Find out which indexes are selected.
                    int minIndex = lsm.getMinSelectionIndex();
                    int maxIndex = lsm.getMaxSelectionIndex();
     
                    for (int i =minIndex; i <= maxIndex; i++) {
    //                    if (lsm.isSelectedIndex(i)) {
     
    //                        Object item = list2.getModel().getElementAt(i);
    ////                        System.out.println(item);
    //                        output.append(item.toString());
    //                       output.append(newline);    
    //                      output.repaint();
    //                      output.revalidate();
     
    //                }
                }
     
            }
        }
     
        public class ReadFile extends Thread {
     
            private String fileName = null;
            private int bufferSize = 1000000000;
     
            public ReadFile() {
            }
     
            public ReadFile(String fileName, int bufferSize) {
                this.setFileName(fileName);
                this.setBufferSize(bufferSize);
     
            }
     
            public ReadFile(String fileName) {
                this(fileName, 1000);
            }
     
            public void setFileName(String fileName) {
                this.fileName = fileName;
            }
     
            public String getFileName() {
                return this.fileName;
            }
     
            public void setBufferSize(int bufferSize) {
                this.bufferSize = bufferSize;
            }
     
            public int getBufferSize() {
                return this.bufferSize;
            }
     
            public ArrayList read() throws java.io.FileNotFoundException, java.io.IOException {
                FileReader fr = new FileReader(this.getFileName());
                BufferedReader br = new BufferedReader(fr);
                ArrayList aList = new ArrayList(this.getBufferSize());
     
                String line = null;
                while ((line = br.readLine()) != null) {
                    aList.add(line);
    //              int n = aList.size();
                }
    //                 int n =+1;
                 setCount(aList.size());
                    System.out.println(aList.size());
                    System.out.println(getCount());
                br.close();
                fr.close();
     
                return aList;
            }
        }
     
     
    }


    import java.io.*;
     
     
    public class TruffyLog {
     
        public static boolean isOn() {
            return on;
        }
     
        public static void setOn(boolean aOn) {
            on = aOn;
        }
     
     
        /**
         * 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();
    //      GW tryMe = new GW();
             if(isOn() == false){
             System.out.println("NEW GUI");
             ex.createAndShowGUI();
                setOn(true);
             }
             else if(isOn() == true){
                 System.out.println("REFRESH GUI");
                 ex.refreshGUI();
             }
     
         }
     
     
        public static void main(String[] args) throws Exception{
            System.out.println("Watching file");
     
            TruffyLog tl = new TruffyLog();
            long size = tl.getFileSize("c:\\a.txt");
            StartSize = size;
            System.out.println("Start size: " + StartSize);
     
            while(true){
    //       getFiles();
            size = tl.getFileSize("c:\\a.txt");
            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);
            }
     
        }

    the boolean on is not working. So it is always reading the whole file. please help.
    Attached Files Attached Files
    • File Type: txt a.txt (148 Bytes, 1 views)

  10. #34
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: getting files in the linux server

    Hey Truffy.

    In the TruffyLog class, try changing:

     public static boolean isOn() {
            [B]return on;[/B]
        }
    to

     public static boolean isOn() {
            [B]return true;[/B]
        }
    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.

  11. #35
    Member Truffy's Avatar
    Join Date
    May 2009
    Location
    Philippines
    Posts
    93
    Thanks
    2
    Thanked 9 Times in 7 Posts

    Default Re: getting files in the linux server

    Quote Originally Posted by JavaPF View Post
    Hey Truffy.

    In the TruffyLog class, try changing:

     public static boolean isOn() {
            [B]return on;[/B]
        }
    to

     public static boolean isOn() {
            [B]return true;[/B]
        }
    Hi,

    Good morning JavaPF.

    I already solved it. but i got a problem when i get the count.

    1. The scenario of it is: when the file change: the GUI will show for the first time, so it will show all the details in the a.txt.
    2. When the a.txt change again, it will go to the REFRESH GUI, then in the ex.java, it will got to the else cndition and only shows the additional data in the a.txt.

    But the prob is, it didn't show in the GUI. It is balnk.

  12. #36
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: getting files in the linux server

    Quote Originally Posted by Truffy

    I already solved it. but i got a problem when i get the count.

    1. The scenario of it is: when the file change: the GUI will show for the first time, so it will show all the details in the a.txt.
    2. When the a.txt change again, it will go to the REFRESH GUI, then in the ex.java, it will got to the else cndition and only shows the additional data in the a.txt.

    But the prob is, it didn't show in the GUI. It is balnk.
    Can you post your updated code for us to look at then please...
    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.

  13. #37
    Member Truffy's Avatar
    Join Date
    May 2009
    Location
    Philippines
    Posts
    93
    Thanks
    2
    Thanked 9 Times in 7 Posts

    Default Re: getting files in the linux server

    Quote Originally Posted by JavaPF View Post
    Can you post your updated code for us to look at then please...
    import java.io.*;
     
     
    public class TruffyLog {
     
        public static long getTryU() {
            return tryU;
        }
     
        public static String getTryStr() {
            return tryStr;
        }
     
        public static void setTryStr(String aTryStr) {
            tryStr = aTryStr;
        }
     
     
         public static long StartSize;
         private static  boolean on   = false;
         private static String tryStr="false";
         private static long tryU;
     
     
         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");
     
             if(on == false){
             System.out.println("NEW GUI");
             ex.createAndShowGUI();
             on=true;
             tryStr = "false";
             setTryStr("false");
             }
             else if(on == true){
                 setTryStr("true");
                  tryStr = "true";
                 System.out.println("REFRESH GUI");
                 ex.refreshGUI();
             }
     
         }
     
     
        public static void main(String[] args) throws Exception{
            System.out.println("Watching file");
     
            TruffyLog tl = new TruffyLog();
            long size = tl.getFileSize("c:\\a.txt");
            StartSize = size;
            System.out.println("Start size: " + StartSize);
            while(true){
            size = tl.getFileSize("c:\\a.txt");
            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 aa.GW.ReadFile;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.table.*;
     
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
     
    public class ex extends JPanel {
     
        JTextArea output;
        JList list = new JList();
        JList list2;
        JTable table  ;
        String newline = "\n";
        ListSelectionModel listSelectionModel;
        ListSelectionModel listSelectionModel2;
        JScrollPane listPane;
        JScrollPane listPane2;
        DefaultListModel listModel = new DefaultListModel();
        DefaultListModel listModel2 = new DefaultListModel();
        ListModel bigData;
        String out3 = "";// = new String[1000];
        int aa = 0;
        int index = 0;
        int index2 = 0;
        String[] lna;
        String out1 = " ";
        String out2 = " ";
        String out = " ";
        String out4 = " ";
        String out5 = " ";    
        String out6 = " ";   
        private int count;
        public static JFrame frame = new JFrame("GW");
        private static String on = TruffyLog.getTryStr();
     
     
        public ex() {
            super(new BorderLayout());
            System.out.println(TruffyLog.getTryStr());
            list = new JList(listModel);
            index = list.getSelectedIndex(); //get selected index
            listSelectionModel = list.getSelectionModel();
            listSelectionModel.addListSelectionListener(
                              new SharedListSelectionHandler());
            listPane = new JScrollPane(list,
                                       ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
                                       ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
     
            list2 = new JList(listModel2);
            list2.repaint();
            list2.validate();
     
            //Build output area.
            output = new JTextArea(1, 10);
            output.setEditable(false);
            JScrollPane outputPane = new JScrollPane(output,
                                                     ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
                                                     ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
     
     
            //Do the layout.
            JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
            add(splitPane, BorderLayout.CENTER);
     
            JPanel topHalf = new JPanel();
            topHalf.setLayout(new BoxLayout(topHalf, BoxLayout.LINE_AXIS));
            JPanel listContainer = new JPanel(new GridLayout(1, 1));
            listContainer.setBorder(BorderFactory.createTitledBorder(
                    "GW"));
            listContainer.add(listPane);
     
            topHalf.setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 5));
    //        topHalf.add(listContainer);
            topHalf.add(listPane);
    //        topHalf.add(tableContainer);
     
            topHalf.setMinimumSize(new Dimension(100, 50));
            topHalf.setPreferredSize(new Dimension(100, 110));
            splitPane.add(topHalf);
     
            JPanel bottomHalf = new JPanel(new BorderLayout());
            bottomHalf.repaint();
            bottomHalf.validate();
            bottomHalf.add(outputPane, BorderLayout.CENTER);
            //XXX: next line needed if bottomHalf is a scroll pane:
            //bottomHalf.setMinimumSize(new Dimension(400, 50));
            bottomHalf.setPreferredSize(new Dimension(450, 135));
            splitPane.add(bottomHalf);
               bottomHalf.repaint();
            bottomHalf.validate();
     
          try {
            // Create and start the thread
            Thread thread = new ReadFile();
             thread.start();
                ReadFile rf = new ReadFile("c:\\a.txt");
                ArrayList a = rf.read();
                if(TruffyLog.getTryStr().contentEquals("false")){
    //                on = true;
             System.out.println("new read");
                  if (a.size() > 0) {
     
                    int i;
                    for (i = 0; i < a.size(); i++) {
     
                        if (a.contains("========================= INCOMING MESSAGE =========================")) {
                            String test = (String) a.get(i + 1);
                            int idx = 0;
                            String words[] = new String[1000];
                            int tokenCount;
                            StringTokenizer st = new StringTokenizer(test);
                            tokenCount = st.countTokens();
     
                            while (st.hasMoreTokens())
                            {
                                words[idx] = st.nextToken();
                                idx++;
                            }
                            for (idx = 0; idx < tokenCount; idx++) {
     
     
                                    out1 = words[idx]; 
                                    listModel.addElement(out1);
    //                          on = true;
                            }
                        }
                    }
    //                on = true;
                }
                }
                else if(TruffyLog.getTryStr().contentEquals("true")){
                    ArrayList b = rf.read2();
                 System.out.println("get count ======================================================================================");
    //             a.size(getCount());
    //             int x = getCount();
                  if (b.size() > 0) {
     
                    int i;
                    for (i = 0; i < b.size(); i++) {
     
                        if (b.contains("========================= INCOMING MESSAGE =========================")) {
                            String test = (String) a.get(i + 1);
                            int idx = 0;
                            String words[] = new String[1000];
                            int tokenCount;
                            StringTokenizer st = new StringTokenizer(test);
                            tokenCount = st.countTokens();
     
                            while (st.hasMoreTokens()) 
                            {
                                words[idx] = st.nextToken();
     
     
                                    idx++;
                            }
                            out1 = words[idx]; 
                                    listModel.addElement(out1);
                                    listModel2.addElement(out1);
     
                        }
                    }
                }
     
     
                }
     
     
     
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }  
     
               list = new JList(listModel);
               index = list.getSelectedIndex(); //get selected index
               listSelectionModel = list.getSelectionModel();
               listSelectionModel.addListSelectionListener(
               new SharedListSelectionHandler());
               listPane = new JScrollPane(list,
               ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
               ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
     
               list2 = new JList(listModel2);
               list2.repaint();
               list2.validate();
           }
     
     
     
        public static void createAndShowGUI() {
            //Create and set up the window.
     
            frame.dispose();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            //Create and set up the content pane.
            ex demo = new ex();
            demo.setOpaque(true);
            frame.setContentPane(demo);
     
            //Display the window.
            frame.pack();
            frame.setVisible(true);
        }
     
          public static void refreshGUI(){
     
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            //Create and set up the content pane.
            ex demo = new ex();
            demo.setOpaque(true);
            frame.setContentPane(demo);
     
            //Display the window.
            frame.pack();
            frame.setVisible(true);
     
     
        }
     
    //    public static void main(String[] args) {
    //        //Schedule a job for the event-dispatching thread:
    //        //creating and showing this application's GUI.
    //        javax.swing.SwingUtilities.invokeLater(new Runnable() {
    //
    //            public void run() {
    //                createAndShowGUI();
    //            }
    //        });
    //    }
     
        public String getOut3() {
            return out3;
        }
     
        public void setOut3(String out3) {
            this.out3 = out3;
        }
     
        public int getCount() {
            return count;
        }
     
        public void setCount(int count) {
            this.count = count;
        }
     
        class SharedListSelectionHandler implements ListSelectionListener {
     
            public void valueChanged(ListSelectionEvent e) {
                ListSelectionModel lsm = (ListSelectionModel) e.getSource();
     
                    // Find out which indexes are selected.
                    int minIndex = lsm.getMinSelectionIndex();
                    int maxIndex = lsm.getMaxSelectionIndex();
     
                    for (int i =minIndex; i <= maxIndex; i++) {
     
     
     
     
     
                }
     
            }
        }
     
        public class ReadFile extends Thread {
     
            private String fileName = null;
            private int bufferSize = 1000000000;
     
            public ReadFile() {
            }
     
            public ReadFile(String fileName, int bufferSize) {
                this.setFileName(fileName);
                this.setBufferSize(bufferSize);
     
            }
     
            public ReadFile(String fileName) {
                this(fileName, 1000);
            }
     
            public void setFileName(String fileName) {
                this.fileName = fileName;
            }
     
            public String getFileName() {
                return this.fileName;
            }
     
            public void setBufferSize(int bufferSize) {
                this.bufferSize = bufferSize;
            }
     
            public int getBufferSize() {
                return this.bufferSize;
            }
     
            public ArrayList read() throws java.io.FileNotFoundException, java.io.IOException {
                FileReader fr = new FileReader(this.getFileName());
                BufferedReader br = new BufferedReader(fr);
                ArrayList aList = new ArrayList(this.getBufferSize());
     
                String line = null;
    //            br.skip(aa);
                while ((line = br.readLine()) != null) {
                    aList.add(line);
     
                }
     
                 setCount(aList.size());
                 count = aList.size();
                    System.out.println(aList.size());
                    System.out.println(getCount());
                br.close();
                fr.close();
     
                return aList;
            }
     
     
            public ArrayList read2() throws java.io.FileNotFoundException, java.io.IOException {
                FileReader fr = new FileReader(this.getFileName());
                BufferedReader br = new BufferedReader(fr);
                ArrayList aList = new ArrayList(this.getBufferSize());
     
                String line = null;
                br.skip(count);
                while ((line = br.readLine()) != null) {
                    aList.add(line);
     
                }
     
                 setCount(aList.size());
                    System.out.println(aList.size());
                    System.out.println(getCount());
                br.close();
                fr.close();
     
                return aList;
            }
        }
     
     
    }


    pls refer to the earlier post for the a.txt. thanks.

    Cheers,
    Truffy

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Java program which can list all files in a given directory
    By JavaPF in forum Java Programming Tutorials
    Replies: 8
    Last Post: July 9th, 2013, 03:38 AM
  2. FileNotFound error while working with XML files in windows
    By ochieng in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: November 14th, 2008, 07:56 AM

Tags for this Thread