1

I have a text file that is written by Java FileOutputStream.

When i read that file using file_get_contents, then everything is on same line and there are no separators between different strings. I need to know, how to read/parse that file so i have some kind on separators between strings

I'm using somethig like this, to save the file:

Stream stream = new Stream(30000, 30000);
stream.outOffset = 0;
stream.writeString("first string");
stream.writeString("second string");

FileOutputStream out = new FileOutputStream("file.txt");
out.write(stream.outBuffer, 0, stream.outOffset);
out.flush();
out.close();
out = null;
6
  • How does the file look if you read it with Notepad or some other plain editor ? Commented Nov 7, 2010 at 15:00
  • Please show an example of what the file looks like Commented Nov 7, 2010 at 15:02
  • explode ( "\n" , file_get_contents(...) )? Commented Nov 7, 2010 at 15:08
  • Please show an example of what the file looks like, not the Java code to create it Commented Nov 7, 2010 at 15:08
  • Gedit can't open it. It has some weird encoding. But i added my Java code to my question. Commented Nov 7, 2010 at 15:09

3 Answers 3

1

I have no idea what that Stream thing in your code represents, but the usual approach to write String lines to a file is using a PrintWriter.

PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream("/file.txt"), "UTF-8"));
writer.println("first line");
writer.println("second line");
writer.close();

This way each line is separated by the platform default newline, which is the same as you obtain by System.getProperty("line.separator"). On Windows machines this is usually \r\n. In the PHP side, you can then just explode() on that.

Sign up to request clarification or add additional context in comments.

Comments

0

file_get_contents returns the content of the file as a string. There are no lines in a string.

Are you familiar with newlines? See wikipedia

So, what you are probably looking for is either reading your file line for line in PHP, or reading it with file_get_contents like you did and then explode-ing it into lines (use "\n" as separator).

2 Comments

file_get_contents reads the contents of the file as a string, but that means it also reads new line characters. If you print the result of file_get_contents of a string that includes new line characters, then those characters will appear in the printed output.
Yes. He did not state anything on output though. It could even be he’s outputting as HTML, where newlines are pruned white-space so they will not be displayed by the Browser. Not enough detail there from the questioner.
0

There is no indication in your code that you are writing a line separator to the output stream. You need to do something like this:

String nl = System.getProperty("line.separator");

Stream stream = new Stream(30000, 30000);
stream.outOffset = 0;
stream.writeString("first string");
stream.writeString(nl);
stream.writeString("second string");
stream.writeString(nl);

FileOutputStream out = null;

try
{
    out = new FileOutputStream("file.txt");
    out.write(stream.outBuffer, 0, stream.outOffset);
    out.flush();
}
finally
{
    try
    {
        if (out != null)
            out.close();
    }
    catch (IOException ioex) { ; }
}

Using PHP, you can use the explode function to fill an array full of strings from the file you are reading in:

<?php

    $data = file_get_contents('file.txt');
    $lines = explode('\n', $data);

    foreach ($lines as $line)
    {
        echo $line;
    }

?>

Note that depending on your platform, you may need to put '\r\n' for the first explode parameter, or some of your lines may have carriage returns on the end of them.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.