2013-10-07 66 views
-2

我生成这样的文本段落:爪哇 - 检索在文本文件中的文本格式,从字符串创建与FileOutputStream中

>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

你叫什么格式?你期望得到什么,你会得到什么?你如何阅读该文件?提供一个SSCCE。 –

+0

该代码是粗糙的,因为我使用了大约5个不同的类为完整的程序(并不想发布整个事情在这里,太长),基本上我做了一个GUI和使用JFileChooser阅读3文本文件并在文本组件中显示内容,然后将导入的文件转换为字符串以对文本文件执行其他操作 – Conor

+0

这正是您创建和发布SSCCE的原因。 –

回答