开发者

Why does my program not read full files?

开发者 https://www.devze.com 2023-02-09 04:39 出处:网络
I have written code in Java to read the content of a file. But it is working for small line of file only not for more than 1000 line of file. Please tell me me what error I have made in the below prog

I have written code in Java to read the content of a file. But it is working for small line of file only not for more than 1000 line of file. Please tell me me what error I have made in the below program.

program:

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class aaru
{
  public static void main(String args[]) throws FileNotFoundException
  {
    File sourceFile = new File("E:\\parser\\parse3.txt");
    File destinationFile = new File("E:\\parser\\new.txt");
    FileInputStream fileIn = new FileInputStream(sourceFile);
    FileOutputStream fileOut = new FileOutputStream(destinationFile);
    DataInputStream dataIn = new DataInputStream(fileIn);
    DataOutputStream dataOut = new DataOutputStream(fileOut);

    String str = "";
    String[] st;
    String sub[] = null;
    String word = "";
    String contents = "";
    String total = "";

    String stri = "";
    try
    {
      while ((contents = dataIn.readLine()) != null)
      {
        total = contents.replaceAll(",", "");
        String str1 = total.replace开发者_运维百科All("--", "");
        String str2 = str1.replaceAll(";", "");
        String str3 = str2.replaceAll("&", "");
        String str4 = str3.replaceAll("^", "");
        String str5 = str4.replaceAll("#", "");
        String str6 = str5.replaceAll("!", "");
        String str7 = str6.replaceAll("/", "");
        String str8 = str7.replaceAll(":", "");
        String str9 = str8.replaceAll("]", "");
        String str10 = str9.replaceAll("\\?", "");
        String str11 = str10.replaceAll("\\*", "");
        String str12 = str11.replaceAll("\\'", "");


        Pattern pattern =
            Pattern.compile("\\s+", Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.MULTILINE);
        Matcher matcher = pattern.matcher(str12);
        //boolean check = matcher.find();
        String result = str12;
        Pattern p = Pattern.compile("^www\\.|\\@");
        Matcher m = p.matcher(result);
        stri = m.replaceAll(" ");

        int i;
        int j;

        st = stri.split("\\.");

        for (i = 0; i < st.length; i++)
        {
          st[i] = st[i].trim();
          /*if(st[i].startsWith(" "))
          st[i]=st[i].substring(1,st[i].length);*/
          sub = st[i].split(" ");

          if (sub.length > 1)
          {
            for (j = 0; j < sub.length - 1; j++)
            {
              word = word + sub[j] + "," + sub[j + 1] + "\r\n";

            }
          }
          else
          {
            word = word + st[i] + "\r\n";
          }
        }
      }

      System.out.println(word);
      dataOut.writeBytes(word + "\r\n");

      fileIn.close();
      fileOut.close();
      dataIn.close();
      dataOut.close();
    } catch (Exception e)
    {
      System.out.print(e);
    }
  }
}


It's not immediately obvious why your code doesn't read full files, but here are two hints:

First: Don't use a DataInputStream for reading full lines. Instead wrap your FileInputStream in a InputStreamReader (ideally providing an encoding) and a BufferedReader (as documented by the JavaDoc of DataInputStream.readLine()):

Like this:

BufferedReader reader = new BufferedReader(new InputStreamReader(fileIn, "UTF-8"));

Second: when you don't know how to handle an exception at least print its stack trace like this:

catch(Exception e)
{
  e.printStackTrace();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消