Quick Search for:  in language:    
RATE,class,contains,Static,methods,allow,user
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
Java/ Javascript Stats

 Code: 220,465. lines
 Jobs: 92. postings

 How to support the site

 
Sponsored by:

 
You are in:
 
Login





Latest Code Ticker for Java/ Javascript.
Click here to see a screenshot of this code!vok - The vocabulary trainer
By Thorsten Stärk on 1/7

(Screen Shot)

Java, Calculator
By Rockwell on 1/4


Eatable Interface
By Rockwell on 1/4


Superclass Person
By Rockwell on 1/4


Draws Cube Function
By Rockwell on 1/4


Rectangle Class
By Rockwell on 1/4


Find Number of Upper and Lower Case Letters in a Command Line Argument String
By Rockwell on 1/4


anagrams
By Rockwell on 1/4


Text Reader with Tokenizer
By Rockwell on 1/4


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!

Affiliate Sites



 
 
   

File Handler

Print
Email
 
VB icon
Submitted on: 7/5/2000 10:58:09 PM
By: Adisson Ruiz 
Level: Advanced
User Rating: By 11 Users
Compatibility:Java (JDK 1.1)

Users have accessed this code 25603 times.
 
 
     The class contains Static methods that allow the class user to work with a file. save, read, save as and open file dialog and some other methods. Please note that both classes are included here. FileHandler and FileFilterExtension. PLEASE RATE IT.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.

//**************************************
//     
// Name: File Handler
// Description:The class contains Static
//     methods that allow the class user to wor
//     k with a file. save, read, save as and o
//     pen file dialog and some other methods. 
//     Please note that both classes are includ
//     ed here. FileHandler and FileFilterExten
//     sion. PLEASE RATE IT.
// By: Adisson Ruiz
//
// Returns:Linkedlists, vectors and stri
//     ng depending on the method invoked.
//
// Assumes:<<<<<<<&
//     lt;<<<<Examples>>>&
//     gt;>>>>>>>>>&
//     gt;
String filePath = FileHandler.OpenFileDialog(this,"Open File","txt","Text files (*.txt)");
LinkedList fileContent = FileHandler.ReadFile(filePath);
ApplicationFileHeader is used to prevent users from opening files that don't belong to your application. A header is placed before the file content.
FileHandler.SaveFile(ApplicationFileHeader,fileContent ,filePath)
FileHandler.FileExists(filePath)
//
//This code is copyrighted and has// limited warranties.Please see http://
//     www.Planet-Source-Code.com/vb/scripts/Sh
//     owCode.asp?txtCodeId=1883&lngWId;=2//for details.//**************************************
//     

/* Copyright: this should not be copied or edited unless with the authors permission
* Company: <P>
* @author: Adisson Ruiz
* Date:15/03/2000
* @version 1.0
*/
/**covers most File handling needs**/
package utilities.File;
import utilities.File.FileFilterExtension;
import javax.swing.*;
import java.awt.Component;
import java.io.*;
import java.util.Vector;
import java.util.LinkedList;
public class FileHandler
    {
    /*****************************************************************
    * Save as FILE DIALOG BOX *
    ******************************************************************/
    public static String saveAsFileDialog(Component Owner,String fileFilterExtension,String fileDescription)
        {
        return saveAsFileDialog(Owner,null,null,' ',fileFilterExtension,fileDescription);
    }

public static String saveAsFileDialog(Component Owner,String Title,String AproveButton,char Mneumonic,String fileFilterExtension,String fileDescription) { JFileChooser chooser = new JFileChooser(); FileFilterExtension filter = new FileFilterExtension(); if (fileFilterExtension != null) { filter.addExtension(fileFilterExtension); chooser.addChoosableFileFilter(filter); chooser.setFileFilter(filter); }
if (fileDescription != null) filter.setDescription(fileDescription); if (Title != null) chooser.setDialogTitle(Title); if (AproveButton != null) chooser.setApproveButtonText(AproveButton); if (Mneumonic != ' ') chooser.setApproveButtonMnemonic(Mneumonic); if (JFileChooser.APPROVE_OPTION == chooser.showSaveDialog(Owner)) return chooser.getSelectedFile().getPath(); return null; }
/***************************************************************** * OPEN FILE DIALOG BOX* ******************************************************************/ public static String OpenFileDialog(Component Owner,String Title) { return OpenFileDialog(Owner,Title,null,' ',null,null); }
public static String OpenFileDialog(Component Owner,String Title,String Extension,String fileDescription) { return OpenFileDialog(Owner,Title,null,' ',Extension,fileDescription); }
public static String OpenFileDialog(Component Owner,String Title,String AproveButton,char Mneumonic,String fileFilterExtension,String fileDescription) { JFileChooser chooser = new JFileChooser(); FileFilterExtension filter = new FileFilterExtension(); if (fileFilterExtension != null) { filter.addExtension(fileFilterExtension); chooser.addChoosableFileFilter(filter); chooser.setFileFilter(filter); }
if (fileDescription != null) filter.setDescription(fileDescription); if (Title != null) chooser.setDialogTitle(Title); if (AproveButton != null) chooser.setApproveButtonText(AproveButton); if (Mneumonic != ' ') chooser.setApproveButtonMnemonic(Mneumonic); if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(Owner)) return chooser.getSelectedFile().getPath(); return null; }
/***************************************************************** * Saves files* ******************************************************************/ public static boolean SaveFile(String FileHeader,Vector FileLines,String tOutputFile) { File OutputFile = new File(tOutputFile); FileWriter CharWriter; try { CharWriter = new FileWriter(OutputFile); }
catch(IOException e) { return false; }
if (FileLines.size()>0) { PrintWriter LineWriter = new PrintWriter(CharWriter); LineWriter.println(FileHeader); for (int x=0;x<FileLines.size();x++) LineWriter.println(FileLines.get(x).toString()); LineWriter.close(); return true; }
return false; }
public static boolean SaveFile(String FileHeader,LinkedList FileLines,String tOutputFile) { File OutputFile = new File(tOutputFile); FileWriter CharWriter; try { CharWriter = new FileWriter(OutputFile); }
catch(IOException e) { return false; }
if (FileLines.size()>0) { PrintWriter LineWriter = new PrintWriter(CharWriter); LineWriter.println(FileHeader); for (int x=0;x<FileLines.size();x++) LineWriter.println(FileLines.get(x).toString()); LineWriter.close(); return true; }
return false; }
public static boolean SaveFile(LinkedList FileLines,String tOutputFile) { File OutputFile = new File(tOutputFile); FileWriter CharWriter; try { CharWriter = new FileWriter(OutputFile); }
catch(IOException e) { return false; }
if (FileLines.size()>0) { PrintWriter LineWriter = new PrintWriter(CharWriter); for (int x=0;x<FileLines.size();x++) LineWriter.println(FileLines.get(x).toString()); LineWriter.close(); return true; }
return false; }
public static boolean SaveFile(String FileLines,String tOutputFile) { File OutputFile = new File(tOutputFile); FileWriter CharWriter; try { CharWriter = new FileWriter(OutputFile); }
catch(IOException e) { return false; }
if (FileLines.length()>0) { PrintWriter LineWriter = new PrintWriter(CharWriter); LineWriter.println(FileLines); LineWriter.close(); return true; }
return false; }
/***************************************************************** * Reads files* ******************************************************************/ public static LinkedList ReadFile(String tInputFile) { File InputFile = new File(tInputFile); FileReader CharReader; LinkedList FileContent = new LinkedList(); String CurrentLine; try { CharReader = new FileReader(InputFile); }
catch(IOException e) { return null; }
BufferedReader Buffer = new BufferedReader(CharReader); do { try { CurrentLine = Buffer.readLine(); if (CurrentLine != null) FileContent.add(CurrentLine); else { FileContent.add(null); Buffer.close(); }
} catch(IOException e) { return FileContent; } }while(CurrentLine != null);
return FileContent; }
public static Vector vReadFile(String tInputFile) { File InputFile = new File(tInputFile); FileReader CharReader; Vector FileContent = new Vector(0); String CurrentLine; try { CharReader = new FileReader(InputFile); }
catch(IOException e) { FileContent.add(null); return FileContent; }
BufferedReader Buffer = new BufferedReader(CharReader); do { try { CurrentLine = Buffer.readLine(); if (CurrentLine != null) FileContent.add(CurrentLine); else { FileContent.add(null); Buffer.close(); }
} catch(IOException e) { return FileContent; } }while(CurrentLine != null);
return FileContent; }
/***************************************************************** *checks file for a header* ******************************************************************/ public static boolean CheckFileHeader(String tInputFile,String tFileHeader) { File InputFile = new File(tInputFile); FileReader CharReader; String FileHeader; try { CharReader = new FileReader(InputFile); }
catch(IOException e) { return false; }
BufferedReader Buffer = new BufferedReader(CharReader); try { FileHeader = Buffer.readLine(); Buffer.close(); } catch(IOException e) { return false; } if (FileHeader != null) return (FileHeader.equals(tFileHeader)); else return false; }
/***************************************************************** * File exists* ******************************************************************/ public static boolean FileExists(String FileName) { File tFile = new File(FileName); return tFile.exists(); }
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- package utilities.File; import javax.swing.filechooser.FileFilter; import java.io.File; import java.util.Hashtable; public class FileFilterExtension extends FileFilter { private String Description = ""; private Hashtable filters = null; FileFilterExtension() { filters = new Hashtable(); setDescription("Unknown File Type"); }
public void addExtension(String Extension) { filters.put(Extension.toLowerCase(),this); }
public void setDescription(String Description) { this.Description = Description; }
public String getDescription() { return Description; }
public boolean accept(File file) { if (file != null) if (file.isDirectory()) return true; String Extension = getExtension(file); if (Extension != null && filters.get(Extension) != null) return true; return false; }
protected String getExtension(File file) { if (file != null) { String FileName = file.getName(); int i = FileName.lastIndexOf('.'); if (1>0 && i < FileName.length() -1) return FileName.substring(i+1).toLowerCase(); }
return null; }
}


Other 6 submission(s) by this author

 

 
Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
Your Vote!

What do you think of this code(in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments
11/20/2001 3:26:25 PM:J.M.Goebel
Does not work with latest Microsoft VM.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this code has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular code, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | Java/ Javascript Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.