LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-22-2004, 11:47 AM   #1
dubya
Member
 
Registered: Mar 2004
Location: Kitchener, Ontario, Canada
Posts: 386

Rep: Reputation: 30
File name formatting program


Hey everyone,
This may seem a little trivial, but here it goes anyway: I'm trying to write a java program that will take in file names as Strings and then look through the name for whitespace and replace it with a _. Reading the file names in is no problem, however then fundamental aspect of changing the characters doesn't work. Here is the code for that part:
Code:
		for (int i = 0; i < args.length; i++)
		{
			System.out.println("Before: " + args[i]);
			args[i] = args[i].replace(' ', '_');
			System.out.println("After : " + args[i]);
		}
where args is an array of strings containing the file names. I know this doesn't physically change the file names, I'm just testing to see if it will actually do it, which it doesn't. Anyone know why? Thanks.
 
Old 11-22-2004, 06:44 PM   #2
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
Hmmm... it works for me:

Code:
Q:\java\test>cat wank.java
class wank {
        public static void main( String[] args ) {

                for (int i = 0; i < args.length; i++)
                {
                        System.out.println("Before: " + args[i]);
                        args[i] = args[i].replace(' ', '_');
                        System.out.println("After : " + args[i]);
                }
        }
}

Q:\java\test>javac wank.java

Q:\java\test>java wank "big fat paulie"
Before: big fat paulie
After : big_fat_paulie
Perhaps it has to do with the way you're running it - are you quoting the files that have spaces in them?
 
Old 11-22-2004, 07:01 PM   #3
dubya
Member
 
Registered: Mar 2004
Location: Kitchener, Ontario, Canada
Posts: 386

Original Poster
Rep: Reputation: 30
Thanks a lot!

One more quick question: do you know of a function that will only change the name of a file and not write an entirely new file? Thanks again.
 
Old 11-22-2004, 07:25 PM   #4
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
I'm not big on java personally, but it looks like the File object has a renameTo() method (see here: http://java.sun.com/j2se/1.3/docs/api/java/io/File.html ).
 
Old 11-22-2004, 08:07 PM   #5
dubya
Member
 
Registered: Mar 2004
Location: Kitchener, Ontario, Canada
Posts: 386

Original Poster
Rep: Reputation: 30
Thanks a lot!
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
File System Formatting carthyc Linux - Newbie 2 08-24-2005 07:06 AM
Fedora Core 4# (Final) hanging on "Formatting/File System"... TippyToes Fedora - Installation 6 07-04-2005 11:06 PM
Batch File for format? Bulk Formatting Beebe Solaris / OpenSolaris 2 04-12-2005 05:07 AM
how to produce a text file from man w/o formatting? spyghost Linux - Newbie 2 07-30-2003 06:05 PM
RedHat 8.0 Installation hanging while formatting /var file system slangley Linux - Distributions 2 02-07-2003 07:34 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration