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

Java File Operations and Palindrome Check

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)
7 views3 pages

Java File Operations and Palindrome Check

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

//copy one file to another file

import [Link].*;
class CopyDataFiletoFile
{
public static void main(String args[])throws IOException
{
FileInputStream Fread =new FileInputStream("[Link]");
FileOutputStream Fwrite=new FileOutputStream("[Link]") ;
[Link]("File is Copied");
int c;
while((c=[Link]())!=-1)
[Link]((char)c);
[Link]();
[Link]();
}
}
-----------
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 [Link].*;

public class Test {


public static void main(String[] args)
throws IOException
{
File file = new File("C:\\Users\\hp\\Desktop\\[Link]");
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 = [Link]()) != null) {


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

public class CodesCracker


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

[Link]("Enter the Name of First File: ");


fileOne = [Link]();
[Link]("Enter the Name of Second File: ");
fileTwo = [Link]();
[Link]("Enter the Name of Third File: ");
fileThree = [Link]();
try
{
FileReader frOne = new FileReader(fileOne);
BufferedReader brOne = new BufferedReader(frOne);
FileReader frTwo = new FileReader(fileTwo);
BufferedReader brTwo = new BufferedReader(frTwo);

for(line=[Link](); line!=null; line=[Link]())


content = content + line + "\n";
[Link]();

for(line=[Link](); line!=null; line=[Link]())


content = content + line + "\n";
[Link]();

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

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

int strLength = [Link]();

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


reverseStr = reverseStr + [Link](i);
}

if ([Link]().equals([Link]())) {
[Link](str + " is a Palindrome String.");
}
else {
[Link](str + " is not a Palindrome String.");
}
}
}

You might also like