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

Java Assignment 9 - 1

The document contains a Java programming assignment with nine tasks related to file handling. Each task includes a code snippet demonstrating different functionalities such as listing files, creating files, reading user input, appending to files, reading file content, transferring file content, storing lines, reading specific lines, and finding the longest word in a file. The assignment is submitted by a student named Vetriselvan K with roll number 2024242018.

Uploaded by

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

Java Assignment 9 - 1

The document contains a Java programming assignment with nine tasks related to file handling. Each task includes a code snippet demonstrating different functionalities such as listing files, creating files, reading user input, appending to files, reading file content, transferring file content, storing lines, reading specific lines, and finding the longest word in a file. The assignment is submitted by a student named Vetriselvan K with roll number 2024242018.

Uploaded by

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

JAVA LABORATORY XC - 3461

ASSIGNMENT - 9

NAME : Vetriselvan K

ROLL NO : 2024242018

BRANCH : [Link] IT

COURSE CODE : XC3461

COURSE NAME : JAVA PROGRAMMING


1. LIST FILES IN A DIRECTORY
import [Link];

public class ListFiles {


public static void main(String[] args) {
File dir = new File("C:/");
String[] files = [Link]();
if (files != null) {
for (String file : files) {
[Link](file);
}
}
}
}

[Link] A NEW FILE AND ADD DATA

import [Link];
import [Link];

public class CreateFile {


public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("[Link]");
[Link]("Hello this is a new file.");
[Link]();

[Link]("File created and data written.");


}
}

[Link] DATA FROM THE USER AND ADD IT TO A NEW FILE

mport [Link];
import [Link];

public class UserToFile {


public static void main(String[] args) throws Exception {
Scanner sc = new Scanner([Link]);

[Link]("Enter text:");
String text = [Link]();
FileWriter fw = new FileWriter("[Link]");
[Link](text);
[Link]();
}
}

[Link] DATA FROM THE USER AND TO THE EXISTING FILE

import [Link];
import [Link];

public class AppendFile {


public static void main(String[] args) throws Exception {
Scanner sc = new Scanner([Link]);

[Link]("Enter text:");
String text = [Link]();

FileWriter fw = new FileWriter("[Link]", true);


[Link](text);
[Link]();
}
}

[Link] DATA FROM THE FILE AND PRINT IT TO CONSOLE

import [Link];
import [Link];

public class ReadFile {


public static void main(String[] args) throws Exception {
File file = new File("[Link]");
Scanner sc = new Scanner(file);

while([Link]()) {
[Link]([Link]());
}
}
}

[Link] CONTENT FROM ONE FILE TO ANOTHER

import [Link].*;

public class CopyFile {


public static void main(String[] args) throws Exception {
FileReader fr = new FileReader("[Link]");
FileWriter fw = new FileWriter("[Link]");

int ch;
while((ch = [Link]()) != -1) {
[Link](ch);
}

[Link]();
[Link]();
}
}

[Link] FILE LINES FROM THE VARIABLE

import [Link];
import [Link];

public class StoreLines {


public static void main(String[] args) throws Exception {
File file = new File("[Link]");
Scanner sc = new Scanner(file);

String content = "";

while([Link]()) {
content += [Link]() + "\n";
}

[Link](content);
}
}

[Link] FIRST 3 LINES OF A FILE

import [Link];
import [Link];

public class FirstThreeLines {


public static void main(String[] args) throws Exception {
File file = new File("[Link]");
Scanner sc = new Scanner(file);

for(int i=1;i<=3 && [Link]();i++) {


[Link]([Link]());
}
}
}

[Link] LONGEST WORD IN A FILE

import [Link];
import [Link];

public class LongestWord {


public static void main(String[] args) throws Exception {
File file = new File("[Link]");
Scanner sc = new Scanner(file);

String longest = "";

while([Link]()) {
String word = [Link]();
if([Link]() > [Link]()) {
longest = word;
}
}

[Link]("Longest word: " + longest);


}
}

You might also like