0% found this document useful (0 votes)
13 views3 pages

Java Programs On Files

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

Java Programs On Files

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

//copy one file to another file

import java.io.*;
class CopyDataFiletoFile
{
public static void main(String args[])throws IOException
{
FileInputStream Fread =new FileInputStream("Hello.txt");
FileOutputStream Fwrite=new FileOutputStream("Hello1.txt") ;
System.out.println("File is Copied");
int c;
while((c=Fread.read())!=-1)
Fwrite.write((char)c);
Fread.close();
Fwrite.close();
}
}
-----------
count no of characters and words in the given file:
// Java program to count the
// number of lines, words, sentences,
// characters, and whitespaces in a file
import java.io.*;

public class Test {


public static void main(String[] args)
throws IOException
{
File file = new File("C:\\Users\\hp\\Desktop\\TextReader.txt");
FileInputStream fileInputStream = new FileInputStream(file);
InputStreamReader inputStreamReader = new
InputStreamReader(fileInputStream);
BufferedReader bufferedReader = new
BufferedReader(inputStreamReader);

String line;
int wordCount = 0;
int characterCount = 0;
int paraCount = 0;
int whiteSpaceCount = 0;
int sentenceCount = 0;

while ((line = bufferedReader.readLine()) != null) {


if (line.equals("")) {
paraCount += 1;
}
else {
characterCount += line.length();
String words[] = line.split("\\s+");
wordCount += words.length;
whiteSpaceCount += wordCount - 1;
String sentence[] = line.split("[!?.:]+");
sentenceCount += sentence.length;
}
}
if (sentenceCount >= 1) {
paraCount++;
}
System.out.println("Total word count = "+ wordCount);
System.out.println("Total number of sentences = "+
sentenceCount);
System.out.println("Total number of characters = "+
characterCount);
System.out.println("Number of paragraphs = "+ paraCount);
System.out.println("Total number of whitespaces = "+
whiteSpaceCount);
}
}
--------------------
//merge two files into third file
import java.io.*;
import java.util.Scanner;

public class CodesCracker


{
public static void main(String[] args)
{
String fileOne, fileTwo, fileThree, line, content="";
Scanner scan = new Scanner(System.in);

System.out.print("Enter the Name of First File: ");


fileOne = scan.nextLine();
System.out.print("Enter the Name of Second File: ");
fileTwo = scan.nextLine();
System.out.print("Enter the Name of Third File: ");
fileThree = scan.nextLine();
try
{
FileReader frOne = new FileReader(fileOne);
BufferedReader brOne = new BufferedReader(frOne);
FileReader frTwo = new FileReader(fileTwo);
BufferedReader brTwo = new BufferedReader(frTwo);

for(line=brOne.readLine(); line!=null; line=brOne.readLine())


content = content + line + "\n";
brOne.close();

for(line=brTwo.readLine(); line!=null; line=brTwo.readLine())


content = content + line + "\n";
brTwo.close();

try
{
FileWriter fw = new FileWriter(fileThree, true);
fw.write(content);
fw.close();
System.out.println("\nSuccessfully merged the content of two files
into the third file");
}
catch(IOException ioe)
{
System.out.println("\nSomething went wrong!");
System.out.println("Exception: " +ioe);
}
}
catch(IOException ioe)
{
System.out.println("\nSomething went wrong!");
System.out.print("Exception: " +ioe);
}
}
}
-------------------
//String Pallendrom or not
class Main {
public static void main(String[] args) {

String str = "Radar", reverseStr = "";

int strLength = str.length();

for (int i = (strLength - 1); i >=0; --i) {


reverseStr = reverseStr + str.charAt(i);
}

if (str.toLowerCase().equals(reverseStr.toLowerCase())) {
System.out.println(str + " is a Palindrome String.");
}
else {
System.out.println(str + " is not a Palindrome String.");
}
}
}

You might also like