is that anybody help me in java program . to reading the file using the array in
ID: 3668432 • Letter: I
Question
is that anybody help me in java program . to reading the file using the array index p1artists.txt
here is the p1artists.txt file
Artist ID Artist Name
1 Acconci
2 Ames
3 Aserty
4 Baron
5 Battenberg
Then using the add method to enter the row 6 and 7
here is the sample of my output p2artists2a.txt”
Artist ID Artist Name
1 Acconci
2 Ames
3 Aserty
4 Baron
5 Battenberg
6 Bindner
7 Blain
then using the delete method to delete the Artist ID Artist Name 2
Here is the sample of my output p2artists2b.txt
Artist ID Artist Name
1 Acconci
3 Aserty
4 Baron
5 Battenberg
6 Bindner
7 Blain
Here is the sample of final my output file using A for add and D for delete the file name p2artistis2c.txt”
Action ArtistID Artist Name
A 6 Bindner
A 7 Blain
D 2
A 8 Blum
D 4
A 9 Budd
D 8
Explanation / Answer
package FileHandlingChegg;
import java.awt.List;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Scanner;
public class FileHandler {
public static void add() throws IOException
{
File f=new File("/home/shekhar/Desktop/chegg/CS/java/FileHandling/p1artists");
FileReader fr=new FileReader(f);
File f2=new File("/home/shekhar/Desktop/chegg/CS/java/FileHandling/p2artists");
FileWriter fw=new FileWriter(f2,true);
BufferedReader br=new BufferedReader(fr);
BufferedWriter bw=new BufferedWriter(fw);
ArrayList lines=new ArrayList();
String line=line=br.readLine();
while(line!=null)
{
System.out.println(line);
line=br.readLine();
}
//Add record
Scanner sc=new Scanner(System.in);
String name;
int id;
System.out.println("Enter Artist Name");
name=sc.nextLine();
System.out.println("Enter Artist Id");
id=sc.nextInt();
bw.append(" "+id+" "+name);
bw.close();
fw.close();
}
public static void eraseLast() throws IOException {
File f2=new File("/home/shekhar/Desktop/chegg/CS/java/FileHandling/p2artists");
RandomAccessFile f = new RandomAccessFile(f2, "rw");
byte b;
long length = f.length() - 1;
do {
length -= 1;
f.seek(length);
b = f.readByte();
} while(b != 10);
f.setLength(length+1);
f.close();
}
public static void main(String[] args) throws IOException {
add();
eraseLast();
eraseLast();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.