从zip文件中读取的XML文件中删除空格

时间:2022-04-30 03:03:45

I'm reading in an XML file that's stored inside a zip file. I need to remove the whitespaces, but I nothing seems to work. Here's what I have, but trim() isn't doing anything.

我正在读取存储在zip文件中的XML文件。我需要删除空格,但似乎没什么用。这就是我所拥有的,但trim()没有做任何事情。

        for (Enumeration e = zip.entries(); e.hasMoreElements(); ) {

            ZipEntry entry = (ZipEntry) e.nextElement();
            InputStream inputStream = zip.getInputStream(entry);
            InputStreamReader inputStreamReader = InputStreamReader(inputStream);  
            char[] buffer = new char[1024];
            while (inputStreamReader.read(buffer, 0, buffer.length) != -1) {

                String str = new String(buffer);

                System.out.println(str.trim());
       }

2 个解决方案

#1


trim() will remove any leading or trailing whitespace, but I don't think you can combine the trim command with the System.out.println. Will that capture the results of the trim command?

trim()将删除任何前导或尾随空格,但我认为您不能将trim命令与System.out.println结合使用。是否会捕获trim命令的结果?

Have you tried the following?

你试过以下吗?

String result = str.trim();
System.out.println(result);

If that doesn't work, what about using replaceAll()?

如果这不起作用,那么使用replaceAll()呢?

String result = str.replaceAll(" ","");
System.out.println(result);

Or if its not a simple whitespace character, try something like this that removes more than one whitespace?

或者,如果它不是一个简单的空白字符,尝试这样的东西,删除多个空格?

String result = str.replaceAll("\\s+","");
System.out.println(result);

#2


This should resolve your issue. Opening a zip --> Reading a file --> Trimming a file.

这应该可以解决您的问题。打开zip - >读取文件 - >修剪文件。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 *
 * @author comrench
 */
public class XMLTrim {
    ZipEntry zipEntry;
    Enumeration enumEntries;
    BufferedReader bufferedReader;
    InputStreamReader inputStreamReader;
    ZipFile zip;
    private String line=null;
    public static void main(String[] args){
        XMLTrim xmlTrim= new XMLTrim();
        xmlTrim.trim();
    }

    public void trim(){
        try {            
            Path tempPath=Paths.get("D:\\share\\tmp.zip");
            zip = new ZipFile(tempPath.toFile());
            Enumeration enumEntries= zip.entries();
            while(enumEntries.hasMoreElements()){
                zipEntry = (ZipEntry) enumEntries.nextElement();
                readFileContent();
            }
        } catch (IOException ex) {
            System.out.println("exception ex"+ex);
        }
    }

    private void readFileContent() {
        try {
            inputStreamReader = new InputStreamReader(zip.getInputStream(zipEntry));
            bufferedReader=new BufferedReader(inputStreamReader);  
            while ((line = bufferedReader.readLine()) != null) {
                System.out.println(line.trim());
            }
        } catch (IOException e) {
            System.out.println("Exception "+e);
        }            
    }
}

#1


trim() will remove any leading or trailing whitespace, but I don't think you can combine the trim command with the System.out.println. Will that capture the results of the trim command?

trim()将删除任何前导或尾随空格,但我认为您不能将trim命令与System.out.println结合使用。是否会捕获trim命令的结果?

Have you tried the following?

你试过以下吗?

String result = str.trim();
System.out.println(result);

If that doesn't work, what about using replaceAll()?

如果这不起作用,那么使用replaceAll()呢?

String result = str.replaceAll(" ","");
System.out.println(result);

Or if its not a simple whitespace character, try something like this that removes more than one whitespace?

或者,如果它不是一个简单的空白字符,尝试这样的东西,删除多个空格?

String result = str.replaceAll("\\s+","");
System.out.println(result);

#2


This should resolve your issue. Opening a zip --> Reading a file --> Trimming a file.

这应该可以解决您的问题。打开zip - >读取文件 - >修剪文件。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 *
 * @author comrench
 */
public class XMLTrim {
    ZipEntry zipEntry;
    Enumeration enumEntries;
    BufferedReader bufferedReader;
    InputStreamReader inputStreamReader;
    ZipFile zip;
    private String line=null;
    public static void main(String[] args){
        XMLTrim xmlTrim= new XMLTrim();
        xmlTrim.trim();
    }

    public void trim(){
        try {            
            Path tempPath=Paths.get("D:\\share\\tmp.zip");
            zip = new ZipFile(tempPath.toFile());
            Enumeration enumEntries= zip.entries();
            while(enumEntries.hasMoreElements()){
                zipEntry = (ZipEntry) enumEntries.nextElement();
                readFileContent();
            }
        } catch (IOException ex) {
            System.out.println("exception ex"+ex);
        }
    }

    private void readFileContent() {
        try {
            inputStreamReader = new InputStreamReader(zip.getInputStream(zipEntry));
            bufferedReader=new BufferedReader(inputStreamReader);  
            while ((line = bufferedReader.readLine()) != null) {
                System.out.println(line.trim());
            }
        } catch (IOException e) {
            System.out.println("Exception "+e);
        }            
    }
}