1

I want to take some information from a document inside the project text_d.txt.

Now I send "text_d.txt" through a string s2.The file already exists(text_d.txt) but the output file s3,doesn't show anything(empty). The question is: Why it doesn't show anything. Did I write anything wrong?

dict.txt

da yes

profesor teacher

invata learns

java java

zece ten

programare programming

noteaza rates

primeste gets

student student

and text_d.txt

   Student invata programare 
java. Profesor noteaza 
student. Nero primeste zece.

This method is used by the file-writing code:

   public String getTtoD(String _wordtotranslate,String s)throws Throwable

    {
      FileInputStream _dict=new FileInputStream(s);
      BufferedReader _read=new BufferedReader(new InputStreamReader(_dict));
      String _line=_read.readLine();
      char c;
      int sem=0;
      for(int i=0;i<_wordtotranslate.length();i++)
      {
          c=_wordtotranslate.charAt(i);
          if(c>='A' && c<='Z')
              sem=1;

      }
      _wordtotranslate=_wordtotranslate.toLowerCase();
      while(_line!=null)

      {
         String [] _v=_line.split(" ");
         if(_wordtotranslate.equals(_v[1]))
         {
             if(sem==1)
                  {_v[0]=_v[0].substring(0,1).toUpperCase()+_v[0].substring(1);
                  return _v[0];
                  }

             return _v[0];
         }

         _line=_read.readLine();
      }
     _read.close();
     if(sem==1)return             _wordtotranslate=_wordtotranslate.substring(0,1).toUpperCase()+_wordtotranslate.substring(1);
     return _wordtotranslate;

    }

This is the code where the file is written:

       if(dan==1)
     {
      File f=new File(s3);
      f.createNewFile();
      FileInputStream _inputfile=new FileInputStream(s2);
      BufferedReader _readfile=new BufferedReader(new InputStreamReader(_inputfile));
      String _str=_readfile.readLine();
      FileWriter _filewrite=new FileWriter(f);
      BufferedWriter _buffwrt=new BufferedWriter(_filewrite);
      while(_str!=null)
      {
            while (_str != null) {
                String[] _vect = _str.split(" ");
                if(_vect.length>4)throw new WrongSyntax();
                int i;
                for (i = 0; i < _vect.length; i++) {
                    if (_vect[i].contains(".")) {
                         _vect[i]=removeCharAt(_vect[i],_vect[i].indexOf("."));
                        _buffwrt.write(getDtoT(_vect[i],s4) + ".");

                    }

                    else
                        _buffwrt.write(getDtoT(_vect[i],s4) + " ");
                    _buffwrt.flush();
                }
                _buffwrt.flush();
                _str =_readfile.readLine();

            }
            _buffwrt.close();
      }
RealSkeptic
  • 33,993
  • 7
  • 53
  • 79
nnewbie
  • 59
  • 1
  • 1
  • 9
  • 2
    Did you flush the buffer? – kirbyquerby Jan 05 '15 at 18:02
  • [Use `java.nio.files`](http://java7fs.wikia.com/wiki/Using_the_java.nio.file_API), please. And show the code where you write to the file as well – fge Jan 05 '15 at 18:02
  • 2
    If that's all your code, you need to close your streams, which flushes the buffers. – Compass Jan 05 '15 at 18:02
  • http://stackoverflow.com/questions/2340106/what-is-the-purpose-of-flush-in-java-streams – chancea Jan 05 '15 at 18:03
  • @Compass used the flush – nnewbie Jan 05 '15 at 18:09
  • @fge i didn't write it,it is written already(text file) – nnewbie Jan 05 '15 at 18:10
  • 1
    Then what is the `BufferedWriter` for? – fge Jan 05 '15 at 18:11
  • @fge i have a 2 files already written(with text in them) represented as s4("dict.txt") and s2("text.txt"),and i want to write in a third file s3,which i create – nnewbie Jan 05 '15 at 18:17
  • Can you add the content of the text_d.txt file to your question? Also, the code of `getDtoT` and `removeCharAt`. – RealSkeptic Jan 05 '15 at 18:28
  • OK, let me get something clear first; each time you invoke your method you open the three files? That seems like a waste – fge Jan 05 '15 at 18:50
  • @fge yes,well openning 2 and creating the third..The problem is i am not familiar in java with files.I have to send through String [] args(of the main method) string arguments for the text files,thus i am using them in this method – nnewbie Jan 05 '15 at 18:59

0 Answers0