face to java write() problem..pls help

Status
Not open for further replies.

wilfrid100

Newbie level 6
Joined
Nov 27, 2005
Messages
11
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,370
package readtext;

import java.io.*; // For input & output classes
import java.util.Date; // For the Date class

public class Main {

public Main() {
}

public static void main(String[] args)
throws IOException{

BufferedReader in = new BufferedReader(
new FileReader("C:/Documents and Settings/seng/Desktop/testfile/txt.txt"));
/**
*use ' ' as a separator, and rearrange back the datastream column
*/

String text = in.readLine(); // String to be segmented
int count = 0; // Number of substrings
char separator = ' '; // Substring separator

// Determine the number of substrings
int index = 0;
do
{
++count; // Increment count of substrings
++index; // Move past last position
index = text.indexOf(separator, index);
}
while (index != -1);

// Extract the substring into an array
String[] subStr = new String[count]; // Allocate for substrings
index = 0; // Substring start index
int endIndex = 0; // Substring end index
for(int i = 0; i < count; i++)
{
endIndex = text.indexOf(separator,index); // Find next separator

if(endIndex == -1) // If it is not found
subStr = text.substring(index); // extract to the end
else // otherwise
subStr = text.substring(index, endIndex); // to end index

index = endIndex + 1; // Set start for next cycle
}


String dirName = "C:/Documents and Settings/seng/Desktop/testfile"; // Directory name
File aFile = new File(dirName, "data.txt");
aFile.createNewFile(); // Now create a new file if necessary

DataOutputStream out = new DataOutputStream(
new BufferedOutputStream(new FileOutputStream(aFile)));

// Display the substrings
for(int i = 0; i < subStr.length; i++)
{
out.writeChars(subStr); /* here is the problem, i cant print out*/
}
}
}

in tis simple program, i'm want to reading the text from txt.txt file and than save it into another folder which is data.txt file. Now i fail to write on last part (out.writeChars(subStr)). i can get the output display if i overwrite it to "System.out.println(subStr) ". does any1 know how to correct it? i want to write the data into data.txt file...pls help....
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…