Reading a filename from an Excel CSV file EASY 75PTS

Posted on 2005-05-12
Last Modified: 2010-03-31
i want the arg for filereader to be whatever the name of the file is in the specified directory so say for example the file is named "CRS April05 Writeoff.csv"
how would i code so that the app just picks up the file name instead of hardcoding it
into the application

 try {
                    BufferedReader in = new BufferedReader(new FileReader("yourFile.csv"));
                    String str;
                    while ((str = in.readLine()) != null) {
              } catch (IOException e) {
Question by:daveleblanc
    LVL 86

    Expert Comment

    String filename =  "CRS April05 Writeoff.csv";

    BufferedReader in = new BufferedReader(new FileReader(filename));
    LVL 13

    Expert Comment

    Hi daveleblanc,

    u can use the file chooser dialog, as in this tutorial and sample:


    Author Comment


    file name cannot be hardcoded, i basically need it to any file placed in the directory

    is this possible?

    Author Comment

    petmagdy thanks but that will not work as this application is handled via scheduling software, so there is no human interaction when the app is running
    LVL 86

    Accepted Solution

    FilenameFilter filter = new FilenameFilter() {
            public boolean accept(File dir, String name) {
                return name.toLowerCase().endsWith(".csv");
    String[] fileNames = new File(directoryName).list(filter); // (where directoryName is your starting dir)
    String csvFile = fileNames[0]; // range check it though
    LVL 3

    Expert Comment

    public void method(String arg){
    String filename=arg+".csv";
    BufferedReader in = new BufferedReader(new FileReader(filename));

    do i miss something?
    LVL 86

    Expert Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
    Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
    Video by: Michael
    Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
    Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now