0

I have a homework to do and I don't know how to get started. I have to read from an external text file the paths of some random folders. I must make the paths for this folders available even I change the computer.

Then I have to output in the console the number of mp3 files found in every each folder.

My big problem is that I don't know how to make those paths work for every computer on which I run the program and also I don't know how the filter the content.

LATER EDIT: I've managed to write some code. I can search now for the mp3, but... can someone help me with this: how can i add a new path to the txt file from keyboard and also how can i remove an entire line from it?

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class Main {

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

        String ext = ".mp3";

        BufferedReader br = new BufferedReader(new FileReader("Monitor.txt"));
        for (String line; (line = br.readLine()) != null;) {
            findFiles(line, ext);
        }

        br.close();


    }

    private static void findFiles(String dir, String ext) {
        File file = new File(dir);
        if (!file.exists())
            System.out.println(dir + " No such folder folder");
        File[] listFiles = file.listFiles(new FiltruTxt(ext));
        if (listFiles.length == 0) {

            System.out.println(dir + " no file with extension " + ext);
        } else {
            for (File f : listFiles)
                System.out.println("Fisier: " + f.getAbsolutePath());
        }
    }
}


import java.io.File;
import java.io.FilenameFilter;

public class FiltruTxt implements FilenameFilter{

     private String ext;

     public FiltruTxt(String ext){
         this.ext = ext.toLowerCase();
     }
     @Override
     public boolean accept(File dir, String name) {
         return name.toLowerCase().endsWith(ext);
     }

 }
alexoiu
  • 11
  • 3
  • 1
    Welcome to stackoverflow. On this site we try to help with specific programming problems, meaning you have to show some effort yourself. In your case, try writing some code and if you're stuck at some part, show us that code and explain what is not working. – André Stannek Jan 14 '15 at 15:24
  • Fair enough! I'll try to post some code. – alexoiu Jan 14 '15 at 15:28
  • I've added some code! – alexoiu Jan 14 '15 at 23:38

1 Answers1

0

I think that with "available even I change the computer" mean that you need to read the path from the file and not hard code it on your program so if you run in other computer you only need to change the text file and not the program.

But as @André Stannek had said in his comment, you must add to your question what have you tried and what is the exact programming problem you are facing.

When you face a problem, try to divide it in individual and more small problems. For example:

  1. How to read a line from the console?
  2. How to write a new line to a file?

Then try to search for a solution (if you can't think in one). For example in stack overflow, google and of course in the official documentation.

The official documentation:

Some questions in stackoverflow:

Or this links from Internet:

This is the portal of the Java tutorials that you will found very useful when you are learning: http://docs.oracle.com/javase/tutorial/index.html

Community
  • 1
  • 1
PhoneixS
  • 10,574
  • 6
  • 57
  • 73
  • This is a comment not an answer – Mustafa sabir Jan 14 '15 at 15:31
  • The second paragraph yes, but the first is what I think is the answer of his question. I think that "how to make those paths work for every computer" is because he doesn't think that the paths in the text file can change from computer to computer. – PhoneixS Jan 14 '15 at 15:40