Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In java, how can I convert this text file format: // end of file into this text

ID: 3805942 • Letter: I

Question

In java, how can I convert this text file format:

// end of file

into this text file format:

This is how to read the first text file:

The first line of each file below contains the number of vertices and the number of edges in the graph (in the format "n=XXXX m=XXXXX"). The rest of the file is organized as follows: each vertex i appears on a line by itself, followed by a line for each neighbor j>i of i (containing j and the length of edge (i,j)). Each list of neighbors is ended by an empty line. Vertices i which do not have neighbors with an index greater than i are not represented. NOTE: Vertices are indexed from 0 to n-1. NOTE: each edge is mentioned only once with its smaller number endpoint Example: vertex 1 will have vertex neighbors 2, 4, and 5 with edges 5, 9, 6 respectivly.

Explanation / Answer

package com.ve.fileConversion;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FileConversion {

   public static void main(String[] args) throws IOException {

       BufferedReader br = new BufferedReader(new FileReader("D:\file1.txt"));
       try {
           //String builder for creating format
           StringBuilder sb = new StringBuilder();
           //Read line by line from text file
           String line = br.readLine();
           String ver = null;
           while (line != null) {
               //split each and every line by spaces
               String[] strLine = line.toString().split(" ");
               // if line has edge
               if (strLine.length == 1 && !strLine[0].equals("")) {
                   ver = strLine[0];
               }
               //if line has vertex
               else if (ver != null && strLine.length != 1) {
                   String temp = ",";
                   for (String str : strLine) {
                       //Condition to ignore white spaces
                       if (!str.equals("")) {
                           temp = temp + str + ",";
                       }
                   }
                   sb.append(ver + temp.substring(0, temp.length() - 1));
                   sb.append(System.lineSeparator());
               }

               line = br.readLine();

           }
           String newContent = sb.toString();
          
           //Write the new content to file
           BufferedWriter bw = null;
           FileWriter fw = null;
           try {
               fw = new FileWriter("D:\file2.txt");
               bw = new BufferedWriter(fw);
               bw.write(newContent);
           } catch (IOException e) {

           }finally{
               bw.close();
               fw.close();
          
           }

       } finally {
           br.close();
              
       }

   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote