-1

So if the contents of a text file is FCCCC, the output of the same text file should be fcccc.

I have written the following code

  import java.lang.*;
  import java.io.*;
  import java.io.FileWriter;
  import java.io.IOException;
  class A
  {
   public static void main(String args[]) throws IOException
   {
      try 
      {
            FileReader reader = new FileReader("doc1.txt");
            FileWriter writer = new FileWriter("doc1.txt", true);
            int character=' ';
            char m;
            while ((character = reader.read()) != -1) 
            {
                m=Character.toLowerCase((char)character);
                System.out.println(m);
                writer.write(m);
            }
            reader.close();
            writer.close();
      } 
      catch (IOException e) 
      {
        e.printStackTrace();
      }
    }
 }

In the code above if the contents of the text file are FCCCC then the output is FCCCCfcccc.

What is the mistake in the code?

raj
  • 91
  • 3
  • 11
  • is that sysout or in the file you are getting "FCCCCfcccc" ? – voucher_wolves Jun 07 '17 at 04:00
  • Because I think, you are appending into the file thats why ? Try cleaning the content of file first and then write into file. – voucher_wolves Jun 07 '17 at 04:02
  • ok first read all contents from file, process then rewrite again – emotionlessbananas Jun 07 '17 at 04:06
  • When you create a new FileWriter object, the boolean in the constructor determines whether it appends to the file instead of overwriting it. You should either set it to "false" or just use the "(File)" constructor instead of the "(File, boolean)" constructor. – A. Cucci Jun 07 '17 at 05:16

4 Answers4

2

Your writer is writing to the same text file that your reader is reading from.

Try writing to a different text file.

g3muse
  • 121
  • 10
  • True but I want the characters in the same text file to be replaced. – raj Jun 07 '17 at 04:05
  • 1
    If you want to use the same file, possibly look into the answers for [String.replaceAll()](https://stackoverflow.com/questions/23466179/java-replace-specific-string-in-textfile) – g3muse Jun 07 '17 at 04:09
0

Try this :

import java.io.*;

public class TextFile {
    public static void main (String[] args) throws IOException {
            File file1 = new File("intext.txt");
            File file2 = new File("outtext.txt"); 
            char CharCounter = 0;       
            BufferedReader in = (new BufferedReader(newFileReader(file1)));
            PrintWriter out = (new PrintWriter(new FileWriter(file2)));

            int ch;
            while ((ch = in.read()) != -1){

                if (Character.isUpperCase(ch)){
                    Character.toLowerCase(ch);

                }
                out.write(ch);


            }
            in.close();
            out.close();
        }       
    }

and the delete old text and rename the new one.

0

You can use java.io.RandomAccessFile. It allows you to read and write to file and get file pointer and set file pointer. The program:

public static void main(String args[]) throws IOException {
        RandomAccessFile raf = null;
        try {
            raf = new RandomAccessFile("C:\\Untitled.txt", "rw");

            long length = raf.length();
            byte[] buffer = new byte[(int) length];
            long pointer = 0;
            while (raf.read(buffer) != -1) {
                String upperCase = new String(buffer).toUpperCase();
                raf.seek(pointer);
                raf.writeBytes(upperCase);
                pointer = raf.getFilePointer();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (raf != null)
                raf.close();
        }
    }

raf.length() returns number of bytes in file

raf.read(buffer) reads buffer.length amount of bytes into buffer array

raf.seek(pointer); set file pointer to position before current read

raf.getFilePointer(); get file pointer position before next read to be able to write starting from this position.

Jay Smith
  • 2,107
  • 3
  • 11
  • 26
0

I search for my problem to solve my issue and see your code. This is my code and give u in Console like whats you want.

I hope you Enjoy.

import java.io.FileNotFoundException;
import java.util.Scanner;
import java.io.File;



public class WordCounter {
    private static String Say;

    public static void main(String[] args) throws FileNotFoundException {
        File file = new File("Top250imdbMovies.txt");
        Scanner scanner = new Scanner(file);
        while (scanner.hasNextLine()){
           String line = scanner.nextLine();
           Say =  line.toLowerCase();
           System.out.println(Say);
         }
       }
    }