-1

在指定路径上有一个文件,/foo/file-a.txt并且该文件包含另一个文件的路径

file-a.txt包含:/bar/file-b.txt第一行的这条路径。需要解析该文件的路径file-b.txt并压缩该文件,并将该压缩文件移动到/too/我的 Java 代码中的另一个路径。

我一直到下面的代码然后我卡住了。

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Reader 
{

public static void main(String[] args) 
{

    BufferedReader br = null;

    try 
    {

        String CurrentLine;

        br = new BufferedReader(new FileReader("/foo/file-a.txt"));

        while ((CurrentLine = br.readLine()) != null) 
        {
            System.out.println(CurrentLine);
        }

    } 
    catch (IOException e) 
    {
        e.printStackTrace();
    } 
    finally 
    {
        try
        {
            if (br != null)br.close();
        } 
        catch (IOException ex) 
        {
            ex.printStackTrace();
        }
    }

}

}

我正在获取文本路径,我们将不胜感激。提前致谢

4

1 回答 1

2

对于文件的实际压缩页面可能会有所帮助。作为一般说明,此代码将替换当前现有的 zip 文件。

public class TestZip02 {

  public static void main(String[] args) {
    try {
      zip(new File("TextFiles.zip"), new File("sample.txt"));
    } catch (IOException ex) {
      ex.printStackTrace();
    }
  }

  public static void zip(File zip, File file) throws IOException {
    ZipOutputStream zos = null;
    try {
      String name = file.getName();
      zos = new ZipOutputStream(new FileOutputStream(zip));

  ZipEntry entry = new ZipEntry(name);
  zos.putNextEntry(entry);

  FileInputStream fis = null;
  try {
    fis = new FileInputStream(file);
    byte[] byteBuffer = new byte[1024];
    int bytesRead = -1;
    while ((bytesRead = fis.read(byteBuffer)) != -1) {
      zos.write(byteBuffer, 0, bytesRead);
    }
    zos.flush();
  } finally {
    try {
      fis.close();
    } catch (Exception e) {
    }
  }
  zos.closeEntry();

  zos.flush();
} finally {
  try {
    zos.close();
  } catch (Exception e) {
  }
}

} }

移动文件,您可以使用File.renameTo这是一个示例。 希望这可以帮助!

于 2013-11-12T05:45:16.043 回答