34.4k views
4 votes
Write a Java program which reads a text file and writes the content into a new file. During the read-write process, convert all the upper case letters into lower case ones. In other words, your programming task is to create a new file with the same content of the original file, only that all the upper case letters are converted into lower case ones in the new file.

User Nillu
by
4.4k points

1 Answer

4 votes

Answer:

Kindly go to the explanation part for the code you seek.

Step-by-step explanation:

This is the Code to Copy:

package file;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.DataInputStream;

import java.io.FileInputStream;

import java.io.FileWriter;

import java.io.InputStreamReader;

import java.util.Scanner;

public class file

{

public static void main(String []args)

{

try

{

System.out.print("Type a file name: ");

//declare scanner for keyboard input

Scanner reader = new Scanner(System.in);

//scan the input file name

String infilename=reader.nextLine();

//define file stream

FileInputStream fstream = new FileInputStream(infilename);

DataInputStream in = new DataInputStream(fstream);

//define the buffer reader

BufferedReader br = new BufferedReader(new InputStreamReader(in));

String outfilename = "output.txt";

FileWriter fileWriter = new FileWriter(outfilename);

//declare buffer writer for file write

BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);

String strLine,filename;

int status = 0;

int i=0;

//read lines one by one from input file

while ((strLine = br.readLine()) != null)

{

String temp="";

//for each character in a line

for(int k=0;k<strLine.length();k++)

{

char c;

c=strLine.charAt(k);

//if the character is uppercase

if(Character.isUpperCase(c))

//make the character lowercase and append it to the output line

temp +=Character.toLowerCase(c);

else

//character is not lowercase. append to the output line without changing it

temp += c;

}

//write the line to the file

bufferedWriter.write(temp+"\\");

}

//close the input file

in.close();

//close the output file

bufferedWriter.close();

}

catch (Exception e)

{

//System.err.println("Error: " + e.getMessage());

}

}

}

User Shateek
by
5.0k points