-1

I have used FileInputStream, FileOutputStream, DataInputStream and DataoutputStream classes to write the code, which is given below:

import java.io.*;
class Demo
{
    public static void main(String args[]) throws IOException
    {

        File f1=new File("A.txt");
        File f2=new File("Temp.txt");
        if(!f1.exists())
        {
            System.out.print(" file doesn't exist.");
            System.exit(0);
        }
        if(!f2.exists())
        {
            if(!f2.createNewFile())
            {
                System.exit(0);
            }
        }

        FileInputStream fis=new FileInputStream(f1);
        FileOutputStream fos=new FileOutputStream(f2);
        DataInputStream dis=new DataInputStream(fis);
        DataOutputStream dos=new DataOutputStream(fos);
        char ch;
        try
        {
            while(true)
            {
                ch=dis.readChar();
                if(ch>=97 && ch<=122)
                {
                    ch=(char)(ch-32);
                }
                dos.writeChar(ch);
            }
        }
        catch(Exception e1)
        {
            fos.flush();
            fis=new FileInputStream(f2);
            fos=new FileOutputStream(f1);
            dis=new DataInputStream(fis);
            dos=new DataOutputStream(fos);
            try
            {
                while(true)
                {
                    ch=dis.readChar();
                    dos.writeChar(ch);
                }
            }
            catch(Exception e2)
            {
                fos.flush();

            }
            finally
            {
                fis.close();
                fos.close();
            }
        }   
    }
}

But it's not converting lower case to upper case alphabets. Can anybody tell me what's wrong in this code?

Aakash Verma
  • 3,705
  • 5
  • 29
  • 66
  • You can't use `DataInputStream` to read data that wasn't written by a `DataOutputStream`. Try using `FileReader` and `FileWriter` instead. – Kevin Anderson Jul 20 '17 at 03:51
  • @KevinAnderson Interesting! How does the `DataInputStream` get to know whether a file has been written by `DataOutputStream`? – Aakash Verma Jul 20 '17 at 04:10
  • 2
    `DataInputStream` doesn't really "know," as such, that a file was written by `DataOutputStream`; it simply _expects_ the data to be in the specific format that `DataOutputStream` writes, and it will fail in various ways, some subtle, some gross, if the input data is not in the expected format. – Kevin Anderson Jul 20 '17 at 05:03
  • Lovely! Thanks a lot. – Aakash Verma Jul 20 '17 at 06:04
  • Possible duplicate of [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Raedwald Jul 20 '17 at 08:37

2 Answers2

0

You can use BufferedReader and BufferedWriter classes to read a complete line, convert to uppercase and write a whole line.

In case of an exception, I am just printing stack trace to demonstrate usage of buffered reader and writer, you can write the same content in f2 or do whatever as per your requirements.

import java.io.*;

class Demo {
    public static void main(String args[]) throws IOException {

        File f1 = new File("A.txt");
        File f2 = new File("Temp.txt");

        if (!f1.exists()) {
            System.out.print(" file doesn't exist.");
            System.exit(0);
        }

        if (!f2.exists()) {
            if (!f2.createNewFile()) {
                System.exit(0);
            }
        }

        BufferedReader bufferedReader = new BufferedReader(new FileReader(f1));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(f2));
        String line;

        try {
            while ((line = bufferedReader.readLine()) != null) {
                // here you can have counters for no. of lines and no. of 
                // characters for debugging and statistics purpose
                System.out.println(line);
                bufferedWriter.write(line.toUpperCase());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            bufferedReader.close();
            bufferedWriter.close();
        }
    }
}
JRG
  • 4,037
  • 3
  • 23
  • 34
0
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Paths;

public class ConvertLowerToUpper {
    public static void main(String[] args) throws IOException {
        try (PrintWriter output = new PrintWriter("Temp.txt")) {
            Files.readAllLines(Paths.get("A.txt")).stream()
                    .map(String::toUpperCase)
                    .forEach(output::println);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

This code demonstrates a short method for doing this with Java 8 Streams - simply read all lines, map them to the upper case equivalent and print them to the PrintWriter. Add additional error handling to cope with missing files etc and you are done