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....