Java 从文件中删除额外空间
我正在尝试这个代码,但它似乎不起作用 我已经初始化了字符串,但是由于没有初始化它而出错 与replaceAll()相反,任何其他有效的方法()或代码都将受到欢迎。谢谢Java 从文件中删除额外空间,java,Java,我正在尝试这个代码,但它似乎不起作用 我已经初始化了字符串,但是由于没有初始化它而出错 与replaceAll()相反,任何其他有效的方法()或代码都将受到欢迎。谢谢 import java.io.*; class Rspace { public static void main(String args[]) throws IOException { int arr[] = new int[3]; if(arr.length<2)
import java.io.*;
class Rspace
{
public static void main(String args[]) throws IOException
{
int arr[] = new int[3];
if(arr.length<2)
{
System.out.print("Parameters are missing");
System.exit(0);
}
if(arr.length>2)
{
System.out.print("No. of parameters are exceeded");
System.exit(1);
}
File f = new File(args[0]);
if(!f.exists())
{
System.out.print("File does not exists");
System.exit(2);
}
FileInputStream fis = new FileInputStream(args[0]);
FileOutputStream fos = new FileOutputStream(args[1]);
int ch;
String str;
while((ch=fis.read())!=-1) //Reading character & returning ASCII value;shifting to next character
{
fos.write(ch);
str.replaceAll("\\s",""); //Removes all WhiteSpaces
}
fis.close();
fos.close();
}
}
import java.io.*;
类空间
{
公共静态void main(字符串args[])引发IOException
{
int arr[]=新int[3];
如果(平均长度2)
{
系统输出打印(“超出参数数量”);
系统出口(1);
}
文件f=新文件(args[0]);
如果(!f.exists())
{
System.out.print(“文件不存在”);
系统出口(2);
}
FileInputStream fis=新的FileInputStream(args[0]);
FileOutputStream fos=新的FileOutputStream(args[1]);
int-ch;
字符串str;
while((ch=fis.read())!=-1)//读取字符并返回ASCII值;切换到下一个字符
{
fos.write(ch);
str.replaceAll(“\\s”,”);//删除所有空白
}
fis.close();
fos.close();
}
}
如果您想消除空白,可以这样做:
import java.io.*;
class Rspace {
public static void main(String args[]) throws IOException {
int arr[] = new int[3];
if (arr.length < 2) {
System.out.print("Parameters are missing");
System.exit(0);
}
if (arr.length > 2) {
System.out.print("No. of parameters are exceeded");
System.exit(1);
}
File f = new File(args[0]);
if (!f.exists()) {
System.out.print("File does not exists");
System.exit(2);
}
FileInputStream fis = new FileInputStream(args[0]);
FileOutputStream fos = new FileOutputStream(args[1]);
int ch;
boolean lastSpace = false;
while ((ch = fis.read()) != -1) // Reading character & returning ASCII
// value;shifting to next character
{
if (!Character.isWhitespace(ch)) {
fos.write(ch);
lastSpace = false;
} else if (!lastSpace) {
fos.write(ch);
lastSpace = true;
}
}
fis.close();
fos.close();
}
}
import java.io.*;
类空间{
公共静态void main(字符串args[])引发IOException{
int arr[]=新int[3];
如果(arr.length<2){
系统输出打印(“缺少参数”);
系统出口(0);
}
如果(arr.length>2){
系统输出打印(“超出参数数量”);
系统出口(1);
}
文件f=新文件(args[0]);
如果(!f.exists()){
System.out.print(“文件不存在”);
系统出口(2);
}
FileInputStream fis=新的FileInputStream(args[0]);
FileOutputStream fos=新的FileOutputStream(args[1]);
int-ch;
布尔lastSpace=false;
while((ch=fis.read())!=-1)//读取字符并返回ASCII
//值;移动到下一个字符
{
如果(!Character.isWhitespace(ch)){
fos.write(ch);
lastSpace=false;
}else如果(!lastSpace){
fos.write(ch);
lastSpace=true;
}
}
fis.close();
fos.close();
}
}
你不需要带绳子的东西。对于空间,我添加了一个布尔变量来检查我们是否已经有了一个空间。
我希望它能有所帮助。当您调用
replaceAll
时,您认为str
有什么价值?你为什么这么认为?这根本不是问题str
从未被赋值,它保持单位化。您正在读取单个字符,然后突然处理字符串。您可以将str
初始化为空字符串以避免错误:string str=”“
。但是你真的应该回顾一下你正在尝试做什么,因为这只会对空字符串应用replaceAll
。那么有什么建议吗?