0% found this document useful (0 votes)
2 views8 pages

Java I OPrograms

The document contains Java code snippets for various file and folder operations, including creating, deleting, renaming, and listing files and folders. It also includes functionality for reading from and writing to files, as well as counting characters, words, and lines in a file. Each operation is encapsulated in a separate class with a main method to execute the functionality.
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)
2 views8 pages

Java I OPrograms

The document contains Java code snippets for various file and folder operations, including creating, deleting, renaming, and listing files and folders. It also includes functionality for reading from and writing to files, as well as counting characters, words, and lines in a file. Each operation is encapsulated in a separate class with a main method to execute the functionality.
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

1) Folder Creation

import [Link];

class FolderCreation
{
public static void main(String[] args)
{
File f = new File("MyFolder");
if ([Link]())
[Link]("Folder created.");
else
[Link]("Folder already exists.");
}
}

2) Sub Folder Creation


import [Link];

class SubFolderCreation
{
public static void main(String[] args)
{
File f = new File("MyFolder/SubFolder");
if ([Link]())
[Link]("Subfolder created.");
else
[Link]("Subfolder already exists.");
}
}

3) File Creation
import [Link];
import [Link];
class FileCreation
{
public static void main(String[] args) throws IOException
{
File f = new File("[Link]");
if ([Link]())
[Link]("File created.");
else
[Link]("File already exists.");
}
}

4) File Deletion
import [Link];

class FileDeletion
{
public static void main(String[] args)
{
File f = new File("[Link]");
if ([Link]())
[Link]("File deleted.");
else
[Link]("File not found.");
}
}

5) File Rename
import [Link];

class FileRename
{
public static void main(String[] args)
{
File f1 = new File("[Link]");
File f2 = new File("[Link]");
if ([Link](f2))
[Link]("File renamed.");
else
[Link]("Rename failed.");
}
}

6) Printing File Name


import [Link];

class PrintFileName
{
public static void main(String[] args)
{
File f = new File("[Link]");
[Link]("File name: " + [Link]());
}
}

7) Listing all Files and Folders in a Directory


import [Link];

class ListAll
{
public static void main(String[] args)
{
File dir = new File("MyFolder");
String[] list = [Link]();
for (String s : list)
[Link](s);
}
}

8) Listing only Files in a Directory


import [Link];

class ListFiles
{
public static void main(String[] args)
{
File dir = new File("MyFolder");
File[] files = [Link]();
for (File f : files)
if ([Link]())
[Link]([Link]());
}
}

9) Listing only Folders in a Directory


import [Link];

class ListFolders
{
public static void main(String[] args)
{
File dir = new File("MyFolder");
File[] files = [Link]();
for (File f : files)
if ([Link]())
[Link]([Link]());
}
}

10) Listing only .txt files


import [Link];

class ListTxtFiles
{
public static void main(String[] args)
{
File dir = new File("MyFolder");
File[] files = [Link]();
for (File f : files)
if ([Link]() && [Link]().endsWith(".txt"))
[Link]([Link]());
}
}

11) Listing only files having size > 5 MB


import [Link];

class ListLargeFiles
{
public static void main(String[] args)
{
File dir = new File("MyFolder");
File[] files = [Link]();
for (File f : files)
if ([Link]() && [Link]() > 5 * 1024 * 1024)
[Link]([Link]());
}
}

12) File Writing


import [Link];
import [Link];

class FileWriting
{
public static void main(String[] args) throws IOException
{
FileWriter fw = new FileWriter("[Link]");
[Link]("Hello, this is a test file.");
[Link]();
[Link]("Data written.");
}
}
13) File Reading
import [Link];
import [Link];

class FileReading
{
public static void main(String[] args) throws IOException
{
FileReader fr = new FileReader("[Link]");
int i;
while ((i = [Link]()) != -1)
[Link]((char)i);
[Link]();
}
}

14) Count of characters in a file content


import [Link];
import [Link];

class CountChars
{
public static void main(String[] args) throws IOException
{
FileReader fr = new FileReader("[Link]");
int count = 0, i;
while ((i = [Link]()) != -1)
count++;
[Link]();
[Link]("Characters: " + count);
}
}

15) Count of words in a file content


import [Link];
import [Link];
import [Link];

class CountWords
{
public static void main(String[] args) throws FileNotFoundException
{
Scanner sc = new Scanner(new File("[Link]"));
int count = 0;
while ([Link]())
{
[Link]();
count++;
}
[Link]();
[Link]("Words: " + count);
}
}

16) Count of lines in a file content


import [Link];
import [Link];
import [Link];

class CountLines
{
public static void main(String[] args) throws FileNotFoundException
{
Scanner sc = new Scanner(new File("[Link]"));
int count = 0;
while ([Link]())
{
[Link]();
count++;
}
[Link]();
[Link]("Lines: " + count);
}
}

You might also like