Java > Sauvegarde de fichier sous Vista et Seven

TitreSauvegarde de fichier sous Vista et Seven
Postée le12-12-2010
Affichée609
Mini-lien
Description

Permet de rendre vos applications compatible avec les dernier Windows tout en les laissant compatible avec les autres OS

EtatInconnu. Inconnu.
Code d'insertion
Options
Afficher les numéros de lignes  Mettre la source en plein ecran  Selectionner la source  Partager sur Facebook 
Téléchargement Telecharger en format txt  Telecharger en format pdf  Telecharger en format java
Plein ecran
package test;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Properties;

/**
 * @author Jerome Baudoux
 * http://www.jerome-baudoux.com
 */

public class UserDataFile
{
        /**
         * Name of your application
         */

        private static final String APPLICATION_NAME = "MinorityScreen";
       
        /**
         * Subfolder you want to create
         *
         * for exemple :        APPLICATION_NAME/Data
         *                                      APPLICATION_NAME/FichiersRecus
         */

        private static final String [] FOLDERS = {"Data","FichiersRecus"};

       
        /**
         * Creates the UserDataFile Object
         */

        public UserDataFile()  {
               
                // IF YOU ARE ON WINDOWS, MAKE SURE YOUR CONFIGURATION FILES CREATED
                if( System.getProperty("file.separator").equals("\\") ) {
                        for( String folder : FOLDERS ) {
                                new File( System.getenv("APPDATA") + "/" + APPLICATION_NAME + "/" + folder ).mkdirs();         
                        }
                }
        }
       
        /**
         * Get the real path of your file.
         *
         * Exemple : if you are on Windows 7 and you
         * want to get the path of the file :
         *              "./Data/configuration.ms"
         *
         * This fonction will return you :
         *              "C:\User\Toto\AppData\Roaming\MinorityScreen\Data\configuration.ms"
         *
         * @param path path winthin Minority screen
         * @return real path on your computer
         */

        public String getPath( String path ) {
               
                if( System.getProperty("file.separator").equals("\\") ) {
                        return System.getenv("APPDATA") + "/MinorityScreen/" + path;
                } else {
                        return path;
                }
        }
       
        /**
         * Get the file you want regardless of your operating system
         * @param path path
         * @return file
         */

        public File getFile( String path ) {
               
                return new File( getPath(path) );
        }
       
        /**
         * A Little exemple of how to use this class
         * @param args none
         */

        public static void main( String [] args ) {
               
                UserDataFile udf = new UserDataFile();
               
                try {
                        FileOutputStream out = new FileOutputStream( udf.getPath("Data/configurationClient.ms") );
                       
                        Properties prop = new Properties();
                        prop.store(out, "---MinorityScreen Configuration Client---");
                       
                        out.close();
                }
                catch (Exception e)  {
                        e.printStackTrace();
                }
        }
}