PDA

View Full Version : Java Help


Kenshin
04-19-2004, 02:58 AM
Hopefully somone on this forums knows a shit load of Java. I need to know how to write a massive String to a .dat file and also when I access it I want to be able to look at each line as an Array or ArrayList.

WolfmanNCSU
04-19-2004, 04:10 PM
Good thing I know a shit load of JAVA. I programed in it for 4 years while getting my degree. Post the assignment specs, and I will see if I can help you out. Maybe this stuff will help you to start...

File IO is tough at first, but you can make a filereader, bufferreader, and string tokenizer to read from ascii files.


import java.io.*;

private BufferedReader br;

public void openExample(String filename) {
try {
br = new BufferedReader(new FileReader(filename));
}
catch (IOException e) {
System.err.println("Input File Error: " + e.toString());
}
}


Now just use br.readLine() to pull a line of text from your file into the buffer. Break it apart with StringTokenizer on your delimitor and store into array. I hope that helps some.

TuckerXE
04-19-2004, 04:58 PM
Wow - I don't know Java, and that still made some sense to me!

Kenshin
04-20-2004, 03:35 PM
man thanks wolfman i'll try that 3rd period for AP Computer Science.

now does anyone know how to do that in C++ or C for 1st period computer programming class? :D

WolfmanNCSU
04-20-2004, 04:39 PM
Ok, let me try.


#include <fstream.h>
#include <iostream.h>
int main()
{
// create output file and write to it
ofstream a_file("output_file.txt");
a_file<<"This text will now be inside of output_file.txt";
a_file.close();
// closes up the file

// opens input file and gets contents
char str[10];
ifstream b_file("input_file.txt");
b_file>>str; // stores it to string
cout<<str; // displays to user
b_file.close();
// closes up the file
}


That should do it

Kenshin
04-20-2004, 04:58 PM
schweet wolfman I owe you big time.

WolfmanNCSU
04-20-2004, 07:08 PM
No probs, let me know if that all works. I have not been able to test it. I am a little rusty.


Site Sponsors
iPAQ Repair and iPAQ Parts
Market Research News
Market Research Supplier