但是我的字符长度只有十几k啊,所以绝对超不过限制的 我的程序如下: import java.io.*; import java.util.*; public class modifytxt { public static void main(String[] args) { File f = new File("tbl_DotServiceCode2.txt"); File f1 = new File("modify.txt"); try { InputStreamReader read = new InputStreamReader (new FileInputStream(f)); BufferedReader reader=new BufferedReader(read); String line0=""; String line; Properties pp = System.getProperties(); String newLine = pp.getProperty("line.separator"); //String ss=line; //byte[] bytes=new byte[1024]; for(int i=0;;i++) { line=reader.readLine(); if(line==null) break; String[] line1=line.split(","); String[] line2=line1[0].split(" "); String[] line3=line2[0].split("\""); int int1=java.lang.Integer.parseInt(line3[1]); String[] line4=line1[1].split(" "); String[] line5=line4[0].split("\""); int int2=java.lang.Integer.parseInt(line5[1].substring(0,line3[1].length())); if(int1==int2) { line0=line0+line+newLine; //bytes=line.getBytes(); } else { for(int j=0;j<=(int2-int1);j++) { System.out.println(j); String str1=((Integer)(int1+j)).toString(); String str2=str1+line5[1].substring(line3[1].length()); line="\""+str1+" \",\""+str2+" \",\"111 \",\"GSM\""; line0=line0+line+newLine; //bytes=ss.getBytes(); } } } OutputStreamWriter wri = new OutputStreamWriter (new FileOutputStream(f1)); //BufferedWriter writer=new BufferedWriter(wri); wri.write(line0,0,line0.length()); //FileOutputStream fos = new FileOutputStream("modify.txt"); //fos.write(bytes); } catch (FileNotFoundException e) { e.printStackTrace(); } catch(UnsupportedEncodingException e) { e.printStackTrace(); } catch(IOException e) { e.printStackTrace(); } } }
你的 reader 和 wri 没有关!
太感谢bao的回答了,这个问题已解决现在有另一个问题请教一下 String line3[1]="8613719716"; int int1=java.lang.Integer.parseInt(line3[1]);以上是程序中的一小段,但是在运行的过程中总是抛出异常 Exception in thread "main" java.lang.NumberFormatException: For input string: "8613719716"但是把字符减少一位变成"861371971"时又不出抛出异常,我想问下这到底是不是字符长度的问题,或者是其它的原因
int的最大值是2147483647 你的值超过了这个最大值
更正一下,int最大值是2147483646,即2的31次方再减一,Math(2,31)-1
上面的那个代码错在了第二行上,8613719716 这个数字已经超出了 int 类型的范围(-2147483648~2147483647)之间,所以就报异常啰。查看 int 类型的最大值和最小值可以采用 Integer.MAX_VALUE、Integer.MIN_VALUE 来获得,同理 long 类型的也是类似的。也可以这么说,int 类型的最大值也就是 String 类型的最大长度,因为在 String 的源代码中它的长度就是使用 int 类型的。
我的程序如下:
import java.io.*;
import java.util.*;
public class modifytxt
{
public static void main(String[] args)
{
File f = new File("tbl_DotServiceCode2.txt");
File f1 = new File("modify.txt");
try
{
InputStreamReader read = new InputStreamReader (new FileInputStream(f));
BufferedReader reader=new BufferedReader(read);
String line0="";
String line;
Properties pp = System.getProperties();
String newLine = pp.getProperty("line.separator");
//String ss=line;
//byte[] bytes=new byte[1024];
for(int i=0;;i++)
{
line=reader.readLine();
if(line==null)
break;
String[] line1=line.split(",");
String[] line2=line1[0].split(" ");
String[] line3=line2[0].split("\"");
int int1=java.lang.Integer.parseInt(line3[1]);
String[] line4=line1[1].split(" ");
String[] line5=line4[0].split("\"");
int int2=java.lang.Integer.parseInt(line5[1].substring(0,line3[1].length()));
if(int1==int2)
{
line0=line0+line+newLine;
//bytes=line.getBytes();
}
else
{
for(int j=0;j<=(int2-int1);j++)
{
System.out.println(j);
String str1=((Integer)(int1+j)).toString();
String str2=str1+line5[1].substring(line3[1].length());
line="\""+str1+" \",\""+str2+" \",\"111 \",\"GSM\"";
line0=line0+line+newLine;
//bytes=ss.getBytes();
}
}
}
OutputStreamWriter wri = new OutputStreamWriter (new FileOutputStream(f1));
//BufferedWriter writer=new BufferedWriter(wri);
wri.write(line0,0,line0.length());
//FileOutputStream fos = new FileOutputStream("modify.txt");
//fos.write(bytes);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch(UnsupportedEncodingException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
int int1=java.lang.Integer.parseInt(line3[1]);以上是程序中的一小段,但是在运行的过程中总是抛出异常
Exception in thread "main" java.lang.NumberFormatException: For input string: "8613719716"但是把字符减少一位变成"861371971"时又不出抛出异常,我想问下这到底是不是字符长度的问题,或者是其它的原因
你的值超过了这个最大值
java中int是32位,表示不了这么大的数。
你可以使用String 的 parseLong...
抛出异常的点还是一样的
long int1 = Long.parseLong(line3[1]);肯定就可以的,你再试试看。
public class longg
{
public static void main(String[] args)
{
//long g=13800138001;
long g=8613719716;
int j=1; System.out.println(int1+j);
System.out.println("Hello World!");
}
}出错信息
---------- javac ----------
longg.java:6: 过大的整数: 8613719716
long g=8613719716;
^
1 错误输出完成 (耗时: 1 秒) - 正常终止
不是说long可以很长吗?
for(int i=0;;i++)
{
line=reader.readLine();
if(line==null)
break;
String[] line1=line.split(",");
String[] line2=line1[0].split(" ");
String[] line3=line2[0].split("\"");
//String[] line4=line3[1].split("86");
String line4=line3[1].substring(2);
int int1=java.lang.Integer.parseInt(line4);
String[] line5=line1[1].split(" ");
String[] line6=line5[0].split("\"");
String line7=line6[1].substring(2);
int int2=java.lang.Integer.parseInt(line7.substring(0,line4.length()));
System.out.println(line4);
System.out.println(line4.length());
System.out.println(line7.substring(0,line4.length()));出错行在
String line4=line3[1].substring(2);文本中的格式为
"86136461 ","8613646299999 ","111 ","GSM"
BufferedReader br = new BufferedReader(new FileReader("my.txt"));
String line = "";
String newLine = System.getProperty("line.separator");
StringBuffer sb = new StringBuffer();
while ((line = br.readLine()) != null) {
Pattern p = Pattern.compile("^(\")(.*)(\",\")(.*)(\",\")(.*)(\",\")(.*)(\")$");
Matcher matcher = p.matcher(line);
String group1 = "";
String group2 = "";
String group3 = "";
String group4 = "";
while (matcher.find()) {
group1 = matcher.group(2).trim();
group2 = matcher.group(4).trim();
group3 = matcher.group(6).trim();
group4 = matcher.group(8).trim();
}
long num1 = Long.parseLong(group1);
long num2 = Long.parseLong(group2.substring(0, group1.length()));
if (num1 == num2) {
sb.append(line).append(newLine);
} else {
long num2Tail = Long.parseLong(group2.substring(group1.length()));
for (int j = 0; j <= (num2 - num1); j++) {
String str1 = num1 + j + "";
String str2 = str1 + num2Tail;
sb.append("\"").append(str1).append("\"");
sb.append(",\"").append(str2).append("\"");
sb.append(",\"").append(group3).append("\"");
sb.append(",\"").append(group4).append("\"");
sb.append(newLine);
}
}
}
br.close();
BufferedWriter bw = new BufferedWriter(new FileWriter("modify.txt"));
bw.write(sb.toString().trim());
bw.close();
}my.txt 中的测试数据
"86136461","8613646299999","111","GSM"
"861364","8613696299999","111","GSM"生成的 modify.txt 内容:
"86136461","8613646199999","111","GSM"
"86136462","8613646299999","111","GSM"
"861364","8613646299999","111","GSM"
"861365","8613656299999","111","GSM"
"861366","8613666299999","111","GSM"
"861367","8613676299999","111","GSM"
"861368","8613686299999","111","GSM"
"861369","8613696299999","111","GSM"我想大概就是这个意思吧,如果要调试的话,需要将两个全身空格替换成一个 Tab。
continue;
}