This method is used to add words to a txt file and then sort them but I am not allowed use the Buffered Reader Function in my course. Is it possible to use Scanner to do this
public static void Option1Method() throws IOException
{
FileWriter aFileWriter = new FileWriter("wordlist.txt", true);
PrintWriter out = new PrintWriter(aFileWriter);
String word = JOptionPane.showInputDialog(null, "Enter a word");
out.println(word);
out.close();
aFileWriter.close();
String inputFile = "wordlist.txt";
String outputFile = "wordlist.txt";
FileReader fileReader = new FileReader(inputFile);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String inputLine;
List<String> lineList = new ArrayList<String>();
while ((inputLine = bufferedReader.readLine()) != null) {
lineList.add(inputLine);
}
fileReader.close();
Collections.sort(lineList);
FileWriter fileWriter = new FileWriter(outputFile);
PrintWriter out1 = new PrintWriter(fileWriter);
for (String outputLine : lineList) {
out1.println(outputLine);
}
out1.flush();
out1.close();
fileWriter.close();
}
Sure, you can. Here are equivalent codes using BufferedReader and Scanner
public void bufferedReaderVersion() throws Exception{
File f = new File("filePath");
Reader reader = new FileReader(f);
BufferedReader bufferedReader = new BufferedReader(reader);
String line = null;
while((line = bufferedReader.readLine()) != null){
System.out.println(line);
}
bufferedReader.close();
reader.close();
}
public void scannerVersion() throws Exception{
File f = new File("filePath");
Scanner sc = new Scanner(f);
while(sc.hasNext()){
System.out.println(sc.nextLine());
}
sc.close();
}
Related
I have this code but when i press the button its edit only the first line in txt file:
String editN = jTextField13.getText();
if (jTextField18.getText().equals("1")) {
try {
String verify, putData;
File file = new File("Name.txt");
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(editN);
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
while ((verify = br.readLine()) != null) {
if (verify != null) {
putData = verify.replaceAll("here", "there");
bw.write(putData);
}
}
br.close();
bw.close();
FileReader fr2 = new FileReader(file);
BufferedReader br2 = new BufferedReader(fr2);
String line1 = br2.readLine();
br2.close();
Worker1.setText("1. " + line1);
EditWorker.setVisible(false);
Workers.setVisible(true);
Workers.pack();
} catch(IOException e) {
e.printStackTrace();
}
}
What I need to add that it will edit second line?
I need to edit something that it will edit the second line in the txt file.
My problem is that I have a code and I have a problem of fixing the "Cannot find symbol" error. Here's the code.
public static void writer() throws IOException {
FileReader in = null;
FileWriter out = null;
BufferedReader br = null;
BufferedWriter bw = null;
try {
in = new FileReader("Student.txt");
out = new FileWriter("StudentAvg.txt");
br = new BufferedReader(in);
bw = new BufferedWriter(out);
String[] line = new String[28];
line = in.split("\t");
The error is pointed on the splitting process. Is there a problem with my variables?
The problem is, that you haven't read any lines.
br = new BufferedReader(in);
bw = new BufferedWriter(out);
String[] line = new String[28];
line = in.split("\t"); // <-- this is your input file reader.
I think you wanted
br = new BufferedReader(in);
bw = new BufferedWriter(out);
String fromFile;
while ((fromFile = br.readLine()) != null) {
String[] line = fromFile.split("\t");
You might also use a try-with-resources and something like
try (BufferedReader br = new BufferedReader(//
new FileReader("Student.txt"));
BufferedWriter bw = new BufferedWriter(//
new FileWriter("StudentAvg.txt"))) {
String fromFile;
while ((fromFile = br.readLine()) != null) {
String[] line = fromFile.split("\t");
}
}
I want to extract a part of a text file beginning with a String name and ending with an empty line
Here is what I tried:
public File CreateSubscripberFile(File file,String name) throws IOException
{
FileWriter fw = new FileWriter(f);
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String line;
while((line = br.readLine()) != null){
if(line.equals(name))
{
while (((line = br.readLine()) != null) && (!(line.equals("\n"))))
{
fw.write(line);
System.out.println(line);
}
}
}
br.close();
fr.close();
fw.close();
return f;
}
but I get the original file as result!
Try to replace:
(line = br.readLine())!=null)
with:
(!(line = br.readLine()).equals(""))
null in this example would mean 'there is not more lines', and .equals(""), looks for empty line.
it's done by edit he code like this
public File CreateSubscripberFile(File file,String name,String fname) throws IOException
{
File f= new File(fname);
FileWriter fw = new FileWriter(f);
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while((line = br.readLine()) != null){
if(line.equals(name))
{
String line1 = br.readLine() ;
while(line1 != null && !line1.isEmpty() )
{
System.out.println(line1);
line1=br.readLine();
}
}
}
br.close();
fw.close();
return f;
}
I read a file by using bufferreader and also have another array(array_to_compare_with). So, I want to compare these two data.
How can I get over this ? Anybody has a clue ?
String[] array_to_compare_with = { "str1","str2","str3","str4" }
FileInputStream fstream = new FileInputStream("filename");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
// strLine and array_to_compare_with comparison needed to be done
}
Maybe you want something like this:
String[] array_to_compare_with = { "str1","str2","str3","str4" };
FileInputStream fstream = new FileInputStream("filename");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
boolean found = false;
for(String str : array_to_compare_with){
if(str.equalsIgnoreCase(strLine.trim())){
found = true;
break;
}
}
if(found){
//DO WHAT YOU WHANT
}
}
String[] array_to_compare_with = { "str1","str2","str3","str4" }
FileInputStream fstream = new FileInputStream("filename");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
for(String s: array_to_compare_with){
if(strLine.contains(s)){
System.out.println(strLine);
break;
}
}
}
Hi I want to replace the BufferedReader in the piece of code with scanner?? I wrote this code but then realized that we're not allowed use bufferedreader. But Havent a clue how to even go about,
public static void Option1Method() throws IOException
{
FileWriter aFileWriter = new FileWriter("wordlist.txt", true);
PrintWriter out = new PrintWriter(aFileWriter);
String word = JOptionPane.showInputDialog(null, "Enter a word");
out.println(word);
out.close();
aFileWriter.close();
String inputFile = "wordlist.txt";
String outputFile = "wordlist.txt";
FileReader fileReader = new FileReader(inputFile);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String inputLine;
List<String> lineList = new ArrayList<String>();
while ((inputLine = bufferedReader.readLine()) != null) {
lineList.add(inputLine);
}
fileReader.close();
Collections.sort(lineList);
FileWriter fileWriter = new FileWriter(outputFile);
PrintWriter out1 = new PrintWriter(fileWriter);
for (String outputLine : lineList) {
out1.println(outputLine);
}
out1.flush();
out1.close();
fileWriter.close();
}
Take a look at the definitions for hasNextLine and nextLine in the Scanner class.
Although new Scanner(fileReader) works, you could rather pass the file name directly into the Scanner's constructor.