Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS 

Loading a property file from the classpath

Loads a property file located anywhere in the classpath

    private Properties getPropertiesFromClasspath(String propFileName) throws IOException {
        // loading xmlProfileGen.properties from the classpath
        Properties props = new Properties();
        InputStream inputStream = this.getClass().getClassLoader()
            .getResourceAsStream(propFileName);

        if (inputStream == null) {
            throw new FileNotFoundException("property file '" + propFileName
                + "' not found in the classpath");
        }

        props.load(inputStream);

        return props;
    }

Python - getFile Internet

// Scaricare un file dalla rete

package get.file.example;

import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

public class GetFileExample
{
	public GetFileExample()
	{
		try
		{
			byte[] bite = new byte[2048];
			int read;
			URL url = new URL("http://switch.dl.sourceforge.net/sourceforge/pys60miniapps/FlickrS60_src_v0.1b.tar.bz2");
			
			BufferedInputStream bis = new BufferedInputStream(url.openStream());
			FileOutputStream fos = new FileOutputStream("/tmp/file.tar.bz2");
			
			while((read = bis.read(bite))>0)
			{
				fos.write(bite, 0, read);
				System.out.println("--");
			}
			
			fos.close();
			bis.close();
			
			System.out.println("FINE");
			
		}
		catch(MalformedURLException e)
		{
			e.printStackTrace();
		}
		catch(IOException e)
		{
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args)
	{
		new GetFileExample();
	}
}

Reading a text file with StreamReader


  private void ImportCountries()
  {
    string delimiter = ",";
    string fileName = @"c:\countrylist3.csv";

    StreamReader sr = new StreamReader(fileName);

    try
    {
      while (sr.Peek() >= 0)
      {
        string r = sr.ReadLine();
        string[] items = r.Split(delimiter.ToCharArray());
      }
    }
    finally
    {
      sr.Close();
    }
  }

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS