Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   Reading Files in Java problem (http://www.programmingforums.org/showthread.php?t=14938)

domquem Jan 13th, 2008 11:53 PM

Reading Files in Java problem
 
I am new to the Java language::: Any hard core java coders out there!

I need a Simple JAVA snippet that will take a reference file a, that has the following information :

0|TicketType

1|Version

2|TimeStamp

3|TimeZone of TimeStamp

4|Assignment1

5|Assignment2

6|Logical1

7|CorrelationType

8|CorrelationID

9|TicketSequenceID

10|DisconnectBFlag


It should then pick a file b that is delimited with the | character, and print out the various fields as per reference file a.


1|34|2008.01.07 12:21:55|12|4|5-1-1254710754638|abc401|0|00000101A20200000008F8164781EF260C00|0|0|E00318FFFFBFFFFE|254710754638|16|



So that the output is


0|TicketType = 1

1|Version = 34

2|TimeStamp = 2008.01.07 12:21:55

3|TimeZone of TimeStamp = 12

4|Assignment1 = 4

5|Assignment2 = 5-1-1254710754638

6|Logical1 = abc401

7|CorrelationType = 0

8|CorrelationID = 00000101A20200000008F8164781EF260C00

9|TicketSequenceID = 0

10|DisconnectBFlag = 0

many thanks

lectricpharaoh Jan 14th, 2008 4:53 PM

Re: Reading Files in Java problem
 
What's that smell? I think I stepped in something. Smells like either dog shit, or homework. <sniff> Yup, definitely homework. Clean your own shoes off; don't ask us to do it for you.

Translation: Show you've first made an attempt on your own, or you're not likely to receive any help here.

Infinite Recursion Jan 15th, 2008 9:46 AM

Re: Reading Files in Java problem
 
I agree with lectricpharaoh... at least show us you tried to resolve the problem yourself first.

domquem Jan 18th, 2008 7:40 AM

Re: Reading Files in Java problem
 
this is what i have managed to pull.

import java.io.*;
class FileRead
{
public static void main(String args[])
{
try{
// Open the file that is the first command line parameter
FileInputStream fstream = new FileInputStream("a.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println (strLine);
}
//Close the input stream
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
class FileWrite
{
public static void main(String args[])
{
try{
// Create file
FileWriter fstream = new FileWriter("write_me.txt");
BufferedWriter out = new BufferedWriter(fstream);
out.write("create new file and write into it!!/n");
//Close the output stream
out.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}

null_ptr0 Jan 18th, 2008 11:03 AM

Re: Reading Files in Java problem
 
:

  1. import java.io.FileInputStream;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5.  
  6. public class Main {
  7.     private void printOutput() throws AssertionError {
  8.         String[] aLines = getALines();
  9.         String[] bValues = getBValues();
  10.         assert (aLines.length == bValues.length)
  11.         : "Number of lines and values not same";
  12.         for (int i = 0; i < aLines.length; ++i) {
  13.             System.out.println(aLines[i] + " = " + bValues[i]);
  14.         }
  15.     }
  16.  
  17.     public String[] getALines() {
  18.         try {
  19.             BufferedReader bufferedReader =
  20.             new BufferedReader(new FileReader("./a.txt"));
  21.             ArrayList<String> stringList = new ArrayList<String>();
  22.             for (String sLine = bufferedReader.readLine();
  23.             sLine != null; sLine =
  24.               bufferedReader.readLine()) {
  25.                 stringList.add(sLine);
  26.             }
  27.             bufferedReader.close();
  28.             return (String[])
  29.             stringList.toArray(new String[stringList.size()]);
  30.         } catch (IOException ioex) {
  31.             ioex.printStackTrace();
  32.         }
  33.         return null;
  34.     }
  35.  
  36.     public String[] getBValues() {
  37.         try {
  38.             FileInputStream fileInputStream =
  39.             new FileInputStream("./b.txt");
  40.             byte[] buf = new byte[fileInputStream.available()];
  41.             fileInputStream.read(buf, 0, buf.length);
  42.             fileInputStream.close();
  43.             return new String(buf).split("|");
  44.         } catch (IOException ioex) {
  45.             ioex.printStackTrace();
  46.         }
  47.         return null;
  48.     }
  49.  
  50.     public static void main(String[] argv) {
  51.         new Main().printOutput();
  52.     }
  53. }

Heres what 3 minutes gave m.
Study the code a lot, and then rebuild it your own way, and then use that.


All times are GMT -5. The time now is 3:51 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC