Java - 检索文本文件中的文本格式,使用FileOutputStream从字符串创建

时间:2013-10-07 19:47:49

标签: java

我正在生成一个这样的文本段落:

>1.    this is the first line 
>1.    this is the second line
>1.    this is the third line
>1.    this is the fourth line

然后将其存储在字符串中

然后我想将这个字符串输出到一个压缩的文本文档,这一切都有效,但是当我打开用FileOutputStream创建的文本文件时格式不存在,你知道我怎么能检索格式吗?

代码:

try
{
        String toZip = file.toString();

DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("test.zip")));   
ZipOutputStream zos = new ZipOutputStream(dos);


        byte[] b = toZip.getBytes("UTF8");
        ZipEntry entry = new ZipEntry("TheFirstFile.txt");
        System.out.println("Zipping.." + entry.getName());                      
        zos.putNextEntry(entry);        
        zos.write(b, 0, b.length);

        dos.close();
        zos.close();
    }
    catch (IOException e) 
    {
        e.printStackTrace();
    } catch (MyException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

在此处发布代码时遇到问题 测试程序:

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class TestZip {


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

        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("c:\\TEMP\\tests.txt"),"UTF-8"));
        StringBuffer buffer = new StringBuffer();

        String line = br.readLine();

        try
        {
            while(line != null)
            {  
                if(line != null)
                {
                    buffer.append(line + "\n");
                }

                line=br.readLine();
            }

            String data = buffer.toString();
            br.close();

            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("c:\\TEMP\\ZippedFile.zip"));
            ZipOutputStream zos = new ZipOutputStream(bos);


            System.out.println(data);

            byte[] b = data.getBytes();
            ZipEntry entry = new ZipEntry("TheData.txt");
            System.out.println("Zipping.." + entry.getName());                      
            zos.putNextEntry(entry);        
            zos.write(b, 0, b.length);

            zos.closeEntry();
            zos.close();
        }
        catch (IOException e) 
        {
            e.printStackTrace();
        } 
    }
}

当我在控制台上打印文本时,文本被限制在我想要的状态,当我检查压缩文本文件时,文本全部在一行上。

控制台输出: console print result

文本文件结果: result

我希望它在文本文件中准确显示它在控制台中的打印方式。

我从未编写过程序,之前可以读取压缩文本文件,但我试过..

import java.io.FileInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;


public class TestUnzip {


    public static void main (String[] args) { 

        String unzip = ""; 

        try  
        { 
            FileInputStream fin = new FileInputStream("C:\\TEMP\\ZippedFile.zip"); 
            ZipInputStream zin = new ZipInputStream(fin); 
            ZipEntry ze; 

            byte[] bytes = new byte[1024];


            ze = zin.getNextEntry();

            while ((ze = zin.getNextEntry()) != null) 
            { 
                    if (zin.read(bytes, 0, bytes.length) != -1) 
                    {
                        unzip = new String(bytes, "UTF-8");
                    }
                    else 
                    {
                        System.out.println("error");
                    }

                    ze = zin.getNextEntry();
            } 


            System.out.println(unzip.toString());

            zin.closeEntry();
            zin.close();
        } 
        catch(Exception e) 
        { 
            e.printStackTrace();
        }

      } 
}

1 个答案:

答案 0 :(得分:2)

我相信Notepad期待Windows终止。 \r\n,而不只是\n

尝试在另一个编辑器中打开它,例如Notepad ++;

http://en.wikipedia.org/wiki/Newline