⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 config.java

📁 JavaGPS enables access to GPS devices from any Java application. Provides Java API, NMEA0183 parser,
💻 JAVA
字号:
/***********************************************************************
 *  J a v a G P S - GPS access library and Java API                    *
 *  Copyright (C) 2001 Ulrich Walther                                  *
 *                                                                     *
 *  This program is free software; you can redistribute it and/or      *
 *  modify it under the terms of the GNU General Public License as     *
 *  published by the Free Software Foundation; either version 2 of     *
 *  the License, or (at your option) any later version.                *
 *                                                                     *
 *  This program is distributed in the hope that it will be useful,    *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of     *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU   *
 *  General Public License for more details.                           *
 *                                                                     *
 *  You should have received a copy of the GNU General Public          *
 *  License along with this program; if not, write to the Free         *
 *  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,     *
 *  MA 02111-1307 USA                                                  *
 ***********************************************************************/

package org.iu.gps;

import java.io.*;
import java.util.*;
import java.lang.reflect.*;

/**
 *  Various methods for storing/loading Java objects from ASCII-readable
 *  configuration files.
 *
 *@author     walther
 */
public class Config {
	/**
	 * Root path for configuration files.
	 */
	public final static String CONFIG_PATH = "gpsdata/";


	/**
	 *  Gets the name of a configuration file, according to the defined
	 *  CONFIG_PATH.
	 *
	 *@param  name  Parameter
	 *@return       The configFilename value
	 */
	public static String getConfigFilename( String name )
	{
		java.net.URL url = ClassLoader.getSystemResource( CONFIG_PATH + name );
		if ( url != null )
		{
			return url.getFile();
		}

		return CONFIG_PATH + name;
	}


	/**
	 *  Converts string representation into type of given field, stores
	 *  in given object.
	 *
	 *@param  s       Parameter
	 *@param  result  Parameter
	 *@param  f       Parameter
	 */
	public static void convert( String s, Object result, Field f )
	{
		try
		{
			String type = f.getType().getName();

			if ( type.equals( "java.lang.String" ) )
			{
				f.set( result, s );
			}
			else
					if ( type.equals( "byte" ) )
			{
				f.setByte( result, new Byte( s ).byteValue() );
			}
			else
					if ( type.equals( "char" ) )
			{
				f.setChar( result, new String( s ).charAt( 0 ) );
			}
			else
					if ( type.equals( "int" ) )
			{
				f.setInt( result, new Integer( s ).intValue() );
			}
			else
					if ( type.equals( "float" ) )
			{
				f.setFloat( result, new Double( s ).floatValue() );
			}
			else
					if ( type.equals( "double" ) )
			{
				f.setDouble( result, new Double( s ).doubleValue() );
			}
		}
		catch ( Exception e )
		{
//				System.out.println(" *** "+e);
		}
	}


	/**
	 *  Save object in output stream.
	 *
	 *@param  os             Parameter
	 *@param  o              Parameter
	 *@exception  Exception  Exception
	 */
	public static void saveObject( OutputStream os, Object o ) throws Exception
	{
		Properties p = new Properties();

		Class c = o.getClass();
		Field[] f = c.getFields();

		for ( int i = 0; i < f.length; i++ )
		{
			try
			{
				//System.out.println( f[i].getName() + "="+ f[i].get(o) );

				p.put( f[i].getName(), f[i].get( o ).toString() );
			}
			catch ( Exception e )
			{
			}
		}

		p.store( os, c.getName() );
	}


	/**
	 *  Save object in file with given name.
	 *
	 *@param  file           Parameter
	 *@param  o              Parameter
	 *@exception  Exception  Exception
	 */
	public static void saveObject( String file, Object o ) throws Exception
	{
		saveObject( new FileOutputStream( file ), o );
	}


	/**
	 *  Load object from input stream.
	 *
	 *@param  is             Parameter
	 *@param  o              Parameter
	 *@exception  Exception  Exception
	 */
	public static void loadObject( InputStream is, Object o ) throws Exception
	{
		Properties p = new Properties();
		p.load( is );

		Class c = o.getClass();
		Field[] f = c.getFields();

		for ( int i = 0; i < f.length; i++ )
		{
			try
			{
				//System.out.println( f[i].getName() + "="+ f[i].get(o) );

				String s = ( String ) p.get( f[i].getName() );
				convert( s, o, f[i] );
			}
			catch ( Exception e )
			{
			}
		}
	}


	/**
	 *  Load object from file with given name.
	 *
	 *@param  file           Parameter
	 *@param  o              Parameter
	 *@exception  Exception  Exception
	 */
	public static void loadObject( String file, Object o ) throws Exception
	{
		loadObject( new FileInputStream( file ), o );
	}


	/**
	 *  The main program for the Config class.
	 *  Stores a GPSInfo object in file "test.dat",
	 *  then reloads object from that file.
	 *
	 *@param  arg            The command line arguments
	 *@exception  Exception  Exception
	 */
	public static void main( String[] arg ) throws Exception
	{
		OutputStream os = new FileOutputStream( "test.dat" );
		ObjectOutputStream oos = new ObjectOutputStream( os );
		GPSInfo gi = new GPSInfo();
		for ( int i = 0; i < 6; i++ )
		{
			oos.writeObject( gi );
		}
		os.close();

		System.out.println( "stored..." );
		System.out.flush();

		InputStream is = new FileInputStream( "test.dat" );
		ObjectInputStream ois = new ObjectInputStream( is );
		for ( int i = 0; i < 6; i++ )
		{
			gi = ( GPSInfo ) ois.readObject();
		}
		is.close();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -