×

Loading...
Ad by
  • 予人玫瑰,手有余香:加拿大新天地工作移民诚聘求职顾问&行业导师!
Ad by
  • 予人玫瑰,手有余香:加拿大新天地工作移民诚聘求职顾问&行业导师!

高手指点,本人初学JAVA

本文发表在 rolia.net 枫下论坛import Java.io.*;
class check_different {
public static void main (String args[]) {
System.out.println("Please enter the directory which your text file located in:");
StringBuffer stfDir = new StringBuffer();
While((char ch=(char)System.in.read())!='\n') { strDir.appendChar(ch); }
File dir = new File(strDir.toString());
File newFile = new File(dir, "Result.txt");
System.out.println("Please input your text file name:");
StringBuffer strFilename = new StringBuffer();
char ch = (char) System.in.read();
While ( ch != '\n' ) { strFilename.appendChar(ch); }
File readFrom = new File(dir,stfFilename.toString());
if(readFrom.isFile()&&readFrom.canRead()) {
RandomAccessFile rafFile = new RandomAccessFile(readFrom, "r");
RandomAccessFile resultFile = new RandomAccessFile(newFile, "rw");
String[] lineString = new String[]{};
StringBuffer strTemp = new StringBuffer();
while(rafFile.getFilePointer()<rafFile.length()) {
if (lineString.length()<2) {
lineString[0] = rafFile.readLine(); }
else {
boolean found = false;
for (int i=0; i<lineString.length(); i++) {
strTemp = rafFile.readLine();
if (lineString[i] == strTemp.toString()) {found = true;}
else { found=false; }
}
if (!found) {lineString[i+1] = strTemp.toString();
resultFile.writeBytes(strTemp.toString());}
}
}
rafFile.close();
resultFile.close();
}
else
System.out.println("File cann't be read!");
}
}更多精彩文章及讨论,请光临枫下论坛 rolia.net
Report

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / 编程问题:如何在一个文本文件(如Txt)中,比较n行字符, 删除重复的字符串行,请见示例:
    This is a test.
    This is a test.
    This is another test.
    This is another test.
    This is another test.

    删除其中重复的内容,变成:
    This is a test.
    This is another test.

    谢谢指点!
    • uinx下应UNIQ就能解决,看看uniq的用法吧,man uniq.
    • 好象没什么难的.
      对每一行比较以前行是否出现过即可. 是则删除, 否则保留.
    • 用正则表达式
    • 要交作业了
    • 嵌套循环
    • sort in.txt | uniq > out.txt
      • 我没有unix, 是Windows/Dos, 用什么语句,能够一行一行的读,然后一行一行的比较?下个星期交作业,不知能不能详诉,先谢过了
        • 用什么语言啊
      • upup
        • 你一年学费多少钱?现在行情一般来说1/12×cost of each course=一次assignment收费,或者50块,取多的。舍不得就好好看书去。这儿没人帮你做作业。
          • 你不乐意帮忙就拉倒,上Rolia就是来相互帮助的,你没有念过书吧?从来没有请教过你的同学?
            • 没兴趣和你吵架,从你问的“我没有unix, 是Windows/Dos, 用什么语句,能够一行一行的读,然后一行一行的比较?”就知道开学到现在没听过课。不就是想弄个现成代码,我帮你up。
              • 有同感,而且又不说用什么语言,想帮也帮不了。
                • 用lilyba(Sunshine)的,SUN+Oracle应该就可以搞定。
                  • 天哪!!!现在硬件是好了,就这点事也不用这么折腾吧!
                    • hehe,我最讨厌写循环了。
                      • 于是要动用数据库。
                        • 我老用数据库。所以,一想就是这一套。
                          • 呵呵,你厉害。
                  • access, sql server都可以。
                    • 你不如说我雇个人一行一行删算了。
                    • 发扬3个代表,向高标准看齐!
                      • 那就选AS/400+ORACLE WAREHOUSE+VIUALAGE FOR JAVA?
                        • HP的成吗?用了好几年了,比较顺手的说。
    • 详细解答见内
      If (you are femal) && (under 30 ) Then
      company me to bar in this saturday night;
      While ( talking, touching, ....);
      you can get your answer
      else
      you have to do it by yourself
      end
      • good good. How did you know candy is a MM
      • revise:
        If (you.getSex()) && (you.getAge() < 30 ) {
        me.ask_companymetobar(time.SATURDAYNIGHT);
        While ( happytime.expire() ) {
        we.talking();
        we.touching();
        happytime.deduct();
        }
        you.getAnswer ();
        } else {
        you.do_it_by_yourself();
        }
        • 还是翠花想的周全
        • Professional
        • If (you.getSex()) && (you.getAge() < 30 )?
          • bug,haha
          • If (you.getSex(sex.female)) && (you.getAge() < 30) && ( you.getAge() > 16) 大家那的法还是要守的。
            • 嗬嗬,写程序泡妹妹?嗯,妹妹逻辑思维不好,全乱了。
              • 面向对象编程法则:找准对象才下手,否则肯定乱。
            • 语法呀语法,同志。
        • While ( ! happytime.expire())
      • 高! 请也参考我的!
        aa = readln(text);
        bb = readln(text);
        if (aa = bb) and ( "you are MM" ) and ( " you invite me dinner" )
        then
        print ( " let's go! " )
        else
        exit
      • :-)
        post your answer
        if answer is right
        If (you are smart & hot & cool) && (under 30 ) Then
        company me to bar this saturday night;
        endif
        else
        you have to do it by yourself
        endif
      • Your statements are not correct. " If (you are femal) && (under 30 ) " but while(false), what is the result?
    • 不知道你用什么语言,原理只能就是这样了
      while (not eof (sourcefile))
      set flag=0
      readline(sourcefile) into tmpline
      while( not eof(destinefile))
      readline(destinefile) into tmp1line
      if tmpline=tmp1line then
      set flag=1
      exit while
      end if
      move the cursor to the next line
      end while
      if flag=0 then
      writeline(tmpline) into destinefile
      end if
      move the cursor to the next line
      end while
    • 我的办法:1生成数据文件,一个表中的一个字段,每行文字作为不同纪录。2用sql,select distinct搞定。
    • use PERL, it only needs 4 lines. PERL is the most powerful language handling text file. ( Code inside )
      #!/usr/bin/perl
      open FILE, "theTestFile.txt" or die "can't open file";
      while (<FILE>) { $a{$_}=1; }
      for (keys %a) { print; }
      close FILE;


      PERL can be run on windows, MAC, unix or linux
      if you have to use Java, check java's hash table class.
      if you have to use C, check oreilly.com
      it has a famous 'Algorithm in C'. Chapter 8 can be free downloaded
      The hash table code is included.

      You don't need to do any sorting. waste time.
      Sorry for the English. I don't have Chinese in my pc
      I have tried the code on our solaris server, it works
      Cheers.
      • I tested your code. It is perfect. Do you know where the perl is used mostly?
    • 我的办法是:把内容放入数组中,然后取出来比较,相同的话删除。
    • 高手指点,本人初学JAVA
      本文发表在 rolia.net 枫下论坛import Java.io.*;
      class check_different {
      public static void main (String args[]) {
      System.out.println("Please enter the directory which your text file located in:");
      StringBuffer stfDir = new StringBuffer();
      While((char ch=(char)System.in.read())!='\n') { strDir.appendChar(ch); }
      File dir = new File(strDir.toString());
      File newFile = new File(dir, "Result.txt");
      System.out.println("Please input your text file name:");
      StringBuffer strFilename = new StringBuffer();
      char ch = (char) System.in.read();
      While ( ch != '\n' ) { strFilename.appendChar(ch); }
      File readFrom = new File(dir,stfFilename.toString());
      if(readFrom.isFile()&&readFrom.canRead()) {
      RandomAccessFile rafFile = new RandomAccessFile(readFrom, "r");
      RandomAccessFile resultFile = new RandomAccessFile(newFile, "rw");
      String[] lineString = new String[]{};
      StringBuffer strTemp = new StringBuffer();
      while(rafFile.getFilePointer()<rafFile.length()) {
      if (lineString.length()<2) {
      lineString[0] = rafFile.readLine(); }
      else {
      boolean found = false;
      for (int i=0; i<lineString.length(); i++) {
      strTemp = rafFile.readLine();
      if (lineString[i] == strTemp.toString()) {found = true;}
      else { found=false; }
      }
      if (!found) {lineString[i+1] = strTemp.toString();
      resultFile.writeBytes(strTemp.toString());}
      }
      }
      rafFile.close();
      resultFile.close();
      }
      else
      System.out.println("File cann't be read!");
      }
      }更多精彩文章及讨论,请光临枫下论坛 rolia.net
      • I am not a programmer, just a system analyst. I am not good at java, but there are 3 obvious errors in your code.
        1
        strTemp = rafFile.readLine();
        should be move out of the for loop.

        2.
        after
        for (int i=0; i<lineString.length(); i++)
        you don't need to [i+1]. because i is already the last item in the array
        ---> if (!found) {lineString[i+1] = strTemp.toString();

        3
        It seems you try to use a 'single loop' to do a 'double loops' job
        it is impossible.

        BTW double loop is very inefficient if the file is big

        Cheers
        • Thanks a lot! But my big problem is : it doesn't work at all! Hahahahaha......
          本文发表在 rolia.net 枫下论坛import Java.io.*;
          class check_different {
          public static void main (String args[]) {
          System.out.println("Please enter the directory which your text file located in:");
          StringBuffer stfDir = new StringBuffer();
          While((char ch=(char)System.in.read())!='\n') { strDir.appendChar(ch); }
          File dir = new File(strDir.toString());
          File newFile = new File(dir, "Result.txt");
          System.out.println("Please input your text file name:");
          StringBuffer strFilename = new StringBuffer();
          char ch = (char) System.in.read();
          While ( ch != '\n' ) { strFilename.appendChar(ch); }
          File readFrom = new File(dir,stfFilename.toString());
          if(readFrom.isFile()&&readFrom.canRead()) {
          RandomAccessFile rafFile = new RandomAccessFile(readFrom, "r");
          RandomAccessFile resultFile = new RandomAccessFile(newFile, "rw");
          String[] lineString = new String[]{};
          StringBuffer strTemp = new StringBuffer();
          while(rafFile.getFilePointer()<rafFile.length()) {
          strTemp = rafFile.readLine();
          if (lineString.length()<2) {
          lineString[0] = strTemp; }
          else {
          boolean found = false;
          for (int i=0; i<lineString.length(); i++) {
          if (lineString[i] == strTemp.toString()) {found = true;}
          else { found=false; }
          }
          if (!found) {lineString[i] = strTemp.toString();
          resultFile.writeBytes(strTemp.toString());}
          }
          }
          rafFile.close();
          resultFile.close();
          }
          else
          System.out.println("File cann't be read!");
          }
          }更多精彩文章及讨论,请光临枫下论坛 rolia.net
          • up!
    • using Perl
      open (IN, "input.txt");
      open (OUT, ">output.txt");
      my $line = "";
      while (<IN>)
      {
      s/^\s+//g;

      if ("$line" ne $_)
      {
      $line = $_;
      print OUT "$line";
      }
      }
      close (OUT);
      close (IN);