Skip to content

Commit 43b743f

Browse files
committed
feat: read and print the contents of a file
Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent d8dc965 commit 43b743f

20 files changed

+370
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import java.io.FileWriter;
2+
import java.io.IOException;
3+
4+
class AppendLogsToFile {
5+
public static void addLogsToFile(String filepath, String log1, String log2){
6+
try {
7+
// Create a FileWriter in append mode
8+
FileWriter writer = new FileWriter(filepath, true);
9+
10+
// Append text to the file
11+
writer.write(log1 + "\n"); // Write log1 and move to new line
12+
writer.write(log2 + "\n"); // Write log2 and move to new line
13+
14+
// Close the writer to save changes
15+
writer.close();
16+
} catch (IOException e) {
17+
System.out.println("An error occurred");
18+
}
19+
}
20+
public static void main (String[] args) throws java.lang.Exception
21+
{
22+
String filepath = "/Users/somesh/Java SE/JavaEvolution-Learning-Growing-Mastering/Section23JavaIOStreams/src/MyJAVA/logs.txt";
23+
String log1 = "log 1";
24+
String log2 = "log 2";
25+
addLogsToFile(filepath, log1, log2);
26+
}
27+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import com.opencsv.CSVWriter;
2+
import java.io.FileWriter;
3+
4+
public class CSVFileWritingInJava {
5+
public static void main(String[] args) {
6+
// Step 1: Path to the CSV file
7+
String filePath = "/Users/somesh/Java SE/JavaEvolution-Learning-Growing-Mastering/Section23JavaIOStreams/src/MyJAVA/data.csv";
8+
9+
try {
10+
// Step 2: FileWriter to write to the file
11+
FileWriter fileWriter = new FileWriter(filePath);
12+
13+
// Step 3: CSVWriter from OpenCSV to write CSV rows
14+
CSVWriter csvWriter = new CSVWriter(fileWriter);
15+
16+
// Step 4: Write header
17+
String[] header = { "Name", "Age", "City" };
18+
csvWriter.writeNext(header);
19+
20+
// Step 5: Write data rows
21+
String[] row1 = { "Alice", "25", "Bangalore" };
22+
String[] row2 = { "Bob", "30", "Delhi" };
23+
String[] row3 = { "Charlie", "22", "Mumbai" };
24+
25+
csvWriter.writeNext(row1);
26+
csvWriter.writeNext(row2);
27+
csvWriter.writeNext(row3);
28+
29+
csvWriter.close();
30+
31+
} catch (Exception e) {
32+
System.out.println("Something went wrong while writing to the file.");
33+
e.printStackTrace();
34+
}
35+
}
36+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import java.io.FileWriter;
2+
import java.io.IOException;
3+
4+
class DailyGratitudeLogger {
5+
public static void addFormattedLinesToFile(String filepath, String line) {
6+
try {
7+
// Create a FileWriter in append mode
8+
FileWriter writer = new FileWriter(filepath, true);
9+
10+
// Convert string to uppercase and write it to the file
11+
writer.write(line.toUpperCase());
12+
writer.write("\n"); // Move to the next line
13+
14+
// Convert string to lowercase and write it to the file
15+
writer.write(line.toLowerCase());
16+
writer.write("\n"); // Move to the next line
17+
18+
// Close the writer to save changes
19+
writer.close();
20+
} catch (IOException e) {
21+
System.out.println("An error occurred");
22+
}
23+
}
24+
25+
public static void main (String[] args) throws java.lang.Exception
26+
{
27+
String filepath = "/home/chef/workspace/output.txt";
28+
String line = "This is a sample line.";
29+
addFormattedLinesToFile(filepath, line);
30+
}
31+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import java.nio.file.Files;
2+
import java.nio.file.Paths;
3+
import java.io.IOException;
4+
import java.util.List;
5+
6+
public class FindLongestCommentLength {
7+
// Method to find length of longest comment
8+
public static int findLongestComment(String filePath) {
9+
try {
10+
// Read all lines from the file
11+
List<String> lines = Files.readAllLines(Paths.get(filePath));
12+
13+
int maxLength = 0;
14+
15+
// Loop through each line to find the longest
16+
for (int i = 0; i < lines.size(); i++) {
17+
int length = lines.get(i).length();
18+
if (length > maxLength) {
19+
maxLength = length;
20+
}
21+
}
22+
23+
return maxLength;
24+
25+
} catch (IOException e) {
26+
System.out.println("Error reading file: " + e.getMessage());
27+
return -1;
28+
}
29+
}
30+
public static void main(String[] args) {
31+
String filePath = "";
32+
System.out.println(findLongestComment(filePath));
33+
}
34+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Name, Age, City
2+
John Doe, 25, New York
3+
Jane Smith, 30, Los Angeles
4+
Alex Johnson, 22, Chicago
5+
Maria Garcia, 27, Miami
6+
David Lee, 35, San Francisco
7+
Emily Davis, 28, Dallas
8+
Michael Brown, 33, Seattle
9+
Sophia Wilson, 24, Boston
10+
James Taylor, 29, Austin
11+
Olivia Martin, 31, Portland
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Name,Designation,Salary
2+
John,Manager,75000
3+
Emma,Developer,55000
4+
Sophia,Designer,60000
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
New York
2+
London
3+
Tokyo
4+
Paris
5+
Sydney
6+
Mumbai
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
hi
2+
Longest Comment
3+
hello
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"Name","Age","City"
2+
"Alice","25","Bangalore"
3+
"Bob","30","Delhi"
4+
"Charlie","22","Mumbai"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"city": "New Delhi",
3+
"temperature": "25C",
4+
"condition": "Clear Skies"
5+
}

0 commit comments

Comments
 (0)