Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Export to Multiple Lists

Status
Not open for further replies.

FAM

Technical User
Jan 13, 2003
345
GB
I have currently got data in a text file structured such as,

0.01 0.01 191650 5 | ae
0.01 0.01 146570 8 | am
0.04 0.04 994907 36 | br
0.23 0.21 5282499 212 | at

i would like to amend the script below to bring each individual column into its own List within a text area,
(So at a later date to sort it via columns) i.e.
-------------------------
|0.01|0.01|191650|5|
|0.01|0.01|146570|8|
-------------------------
Code:
import java.net.*;
import java.io.*;
import java.util.*;

class Files
{
 public static void main(String args[])
 {
   try
   {
      DataInputStream inputFile = new DataInputStream(new FileInputStream("C:\\Clients.txt"));
			
   String data;
   float Reqs;
   float Bytes;
   int BytesSent;
   int Requests;
		
   data = inputFile.readLine();
   while (data != null)
   {
      StringTokenizer st = new StringTokenizer (data);

      Reqs = Float.parseFloat(st.nextToken());
      Bytes = Float.parseFloat(st.nextToken());				
      BytesSent = Integer.parseInt(st.nextToken());
      Requests = Integer.parseInt(st.nextToken());

      System.out.println(Reqs + "\t" + Bytes + "\t" + BytesSent + "\t" + Requests);

      data = inputFile.readLine();
   }
   inputFile.close();
   }
   catch(IOException e)
   {
      System.out.println("Error : " + e);
   }
 }
}
Any suggestions?
Thanks
 
My suggestion is to create a inner class called line with four private attributes, and a toString method. you can easily add another method called getFirstElement(), and then sort the list accroding to this.

After you read each line from the text file, you create an instant of Line. After this, you can call toString method and display it. You need to add a method to sort the list.


Code:
import java.awt.BorderLayout;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.StringTokenizer;

import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JTextPane;

public class ReadFile extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JTextPane jTextPane = null;

	
	/**
	 * This is the default constructor
	 */
	public ReadFile() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(383, 257);
		this.setContentPane(getJContentPane());
		this.setTitle("JFrame");
		readFile();
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJTextPane(), java.awt.BorderLayout.CENTER);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jTextPane	
	 * 	
	 * @return javax.swing.JTextPane	
	 */
	private JTextPane getJTextPane() {
		if (jTextPane == null) {
			jTextPane = new JTextPane();
		}
		return jTextPane;
	}
	
	public void readFile(){
		try {
			DataInputStream inputFile = new DataInputStream(
					new FileInputStream("c://Clients.txt"));

			String data;
			float Reqs;
			float Bytes;
			int BytesSent;
			int Requests;
			String prt = "";
			data = inputFile.readLine();
			while (data != null) {
				StringTokenizer st = new StringTokenizer(data);

				Reqs = Float.parseFloat(st.nextToken());
				Bytes = Float.parseFloat(st.nextToken());
				BytesSent = Integer.parseInt(st.nextToken());
				Requests = Integer.parseInt(st.nextToken());
				line ln = new line(Reqs,Bytes,BytesSent,Requests);
				prt = prt + ln.toString() + "\n";
				System.out.println(Reqs + "\t" + Bytes + "\t" + BytesSent
						+ "\t" + Requests);

				data = inputFile.readLine();
			}
			inputFile.close();
			jTextPane.setText(prt);
		} catch (IOException e) {
			System.out.println("Error : " + e);
		}	
	}
	
	public static void main(String argus[]){
		ReadFile rf = new ReadFile();
		rf.setVisible(true);
	}
	
}  //  @jve:decl-index=0:visual-constraint="10,10"

class line{
	float first;
	float second;
	int third;
	int fourth;
	
	public line(float e1, float e2, int e3, int e4){
		this.first = e1;
		this.second = e2;
		this.third = e3;
		this.fourth = e4;
	}
	
	public String toString(){
		return first + "|" + second + "|" + third + "|" + fourth;
	}
}

Chinese Java Faq Forum
 
Too much stuff for me :)

Code:
   while (data != null)
   {
      StringTokenizer st = new StringTokenizer (data);
      StringBuffer sb = new StringBuffer(data.length);
      for (int i=0;i<4;i++)
        sb.append(st.nextToken().trim()).append("|");
      System.out.println(sb.toString());
      data = inputFile.readLine();
   }

would do the trick.

Cheers,
Dian
 
Thanks for the useful replies, i was just wondering whether splitting the text file into seperate lists within a textarea is the correct way to go about it or whether there is a better way to do it? Ultimately i would like to sort it all via a certain column.

Cheers
 
Make a class Client with bytes, reqs and so on, and let it implement 'Comparable'.
Make a List and add your Clients.
Code:
List list = new ArrayList <Client> ();
// ...
list.add (new Client (data, reqs, bytes, ...);

seeking a job as java-programmer in Berlin:
 
I think the best way is a Table and a TableModel. There are a few examples around, even I think there are some in this forum.

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top