Question: Please help me fix my errors. I would like to read and write the text file in java. my function part do not have errors.

Please help me fix my errors. I would like to read and write the text file in java. my function part do not have errors. below is my code

import java.io.FileInputStream;

import java.io.InputStreamReader;

import java.io.FileWriter;

import java.io.IOException;

public class LinkedList

{

Node head;

class Node

{

int data;

Node next;

Node(int d)

{

data = d;

next = null;

}

}

void printMiddle()

{

Node slow_ptr = head;

Node fast_ptr = head;

if (head != null)

{

while (fast_ptr != null && fast_ptr.next != null)

{

fast_ptr = fast_ptr.next.next;

slow_ptr = slow_ptr.next;

}

System.out.println("The middle element is [" +

slow_ptr.data + "] ");

}

}

public void push(int new_data)

{

Node new_node = new Node(new_data);

new_node.next = head;

head = new_node;

}

public void printList()

{

Node tnode = head;

while (tnode != null)

{

System.out.print(tnode.data+"->");

tnode = tnode.next;

}

System.out.println("NULL");

}

public class TextFileReadingAndWriting {

public static void main(String[] args) {

try {

FileReader reader = new FileReader("MyFile.txt");

try {

FileWriter writer = new FileWriter("MyFile.txt", true);

BufferedReader bufferedReader = new BufferedReader(reader);

String line;

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

System.out.println(line);

}

reader.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

this is string list in a text file which I like to read:

Hishaam Esteban Kevin

Matthew Brandon

Joel Luis Jianwei Yechiel

Taeyong

Jiayu

Jiade Phillip

Russell Mohebullah

Akshar Evgeniia Andres Marco Justin

Robin Kelvin Zhiheng Jeffrey

Yifei Yinyu

Jiaxin Youyia Eleftherios

Yuan

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!