Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Go Back   Code Forums > Application and Web Development > Java
User Name
Password

Reply
 
LinkBack Thread Tools Display Modes
Old 10-28-2004, 04:16 AM   #1 (permalink)
philthee
Registered User
 
Join Date: Oct 2004
Posts: 15
philthee is on a distinguished road
reading files from a directory and printing

Hi there,

can someine give me a hand with the above problem.
I have code here that can read a file from a given path but i would like to be able to read a directory from a given path and do something with the file names.
The file names are of type catA1.cyt, catA1.jpg, catA2.cyt, catA2.jpg etc..

i want to split up the filenames (only interested in the .cyt ones) and keep the A1, A2 etc to put them somewhere.

[code]

/*
* splitString.java
*
* Created on 28 October 2004, 13:05
*/

package Strings;
import java.io.*;
import java.util.*;

/**
*
* @author devallp
*/
public class splitString {

/** Creates a new instance of splitString */
public splitString() {
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
String path = "/mnt/rinetapp1a/users/devallp/src/demoChromosomeStuff
/cat/catA1.cyt";
File catFile = new File(path);
FileInputStream stuff = new FileInputStream(catFile);
FileReader fr = new FileReader(catFile);
BufferedReader in = new BufferedReader(fr);
String line;


while ((line = in.readLine()) != null) {

System.out.println(line);


}
}

catch (FileNotFoundException e) {
System.out.println("File disappeared");
}

catch (java.io.IOException e) {
System.out.println("hmmmmm??");
}
// TODO code application logic here
}

}
[\code]


so this reads a file from a path and prints out the contents.
what would i do firstly to read a directory and print the contents?
__________________

Last edited by philthee : 10-28-2004 at 05:51 AM.
philthee is offline   Reply With Quote
Old 10-28-2004, 07:26 AM   #2 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,114
Belisarius is on a distinguished road
You probably haven't looked at the Javadocs for File. File can either represent a standard file, or a directory. If it's the latter, you can use the list() method to get an array of Strings listing the contents of the directory.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 10-29-2004, 12:51 AM   #3 (permalink)
philthee
Registered User
 
Join Date: Oct 2004
Posts: 15
philthee is on a distinguished road
Quote:
Originally Posted by Belisarius
You probably haven't looked at the Javadocs for File. File can either represent a standard file, or a directory. If it's the latter, you can use the list() method to get an array of Strings listing the contents of the directory.
Cheers, had a wee blast and with some help i got this which works.

Code:
*/import java.io.*; public class retrieveName { /** Creates a new instance of retirveName */ public void listNames(String startName) { File startFile = new File(startName); File[] contents = startFile.listFiles(); if (contents == null) { System.err.println("Could not list " + startFile); return; } java.util.Arrays.sort(contents); for (int i = 0; i < contents.length; i++) { System.out.println(contents[i]); } public static void main(String[] args) { try { ListFileNames myList = new ListFileNames(); myList.listNames("."); //myList.listNames(args[0]); } catch (Exception e) { System.err.println(e); } } }
apologies for the bad indentation.

how do i catch what is in contents[i] and do something with each line of contents?

i.e my results are
./catF1.cyt
./catF1.jpg
./catF2.cyt
./catF2.jpg

i want to take each member of the list (ignoring the jpg files) and save the F1, F2 etc into an array of names.
__________________
philthee is offline   Reply With Quote
Old 10-29-2004, 04:35 AM   #4 (permalink)
philthee
Registered User
 
Join Date: Oct 2004
Posts: 15
philthee is on a distinguished road
ok, i don't know how to join one bit of code onto the next but here is the next bit (kind of) working from a stated string similar to what they will work
Code:
on*/import java.util.*; import java.lang.*; public class SplitLines { public static void main(String[] Args) { SplitLines s = new SplitLines(); } /** Creates a new instance of SplitLines */ public SplitLines() { String text = "catA1.cyt"; String[] fields = text.split("\\W"); for(int x=0;x<1;x++) System.out.println(fields[0]); } }
this splits the sting in to catA1 and .cyt printing just catA1.
not figured out how to get rid of cat yet.
__________________
philthee is offline   Reply With Quote
Old 10-29-2004, 05:01 AM   #5 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,114
Belisarius is on a distinguished road
You'll want to look at the Javadoc for String. Instead of split, you could use endsWith() to figure out which ones end with .cyt. Also, provided all the .cyt files have the same naming scheme, you could use substring to cut off the first 3 letters.
__________________
GitS
Belisarius is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 02:08 AM.


Powered by vBulletin Version 3.6.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle