Tag Archives: read

Read version of package from META-INF in Java JSF Bean

vers
Often you need to show the software version in JSF page.
How to make it as follows:

1. Create JSF Bean

public class PropertiesBean implements Serializable {

    private static final long serialVersionUID = 8607941655014578684L;

    private static final String METAINF_LOCATION = "META-INF/MANIFEST.MF";
    private static final String VERSION_PREFIX = "Implementation-Version: ";
    private static final Pattern VERSION_PATTERN = Pattern.compile("(\\d+\\.\\d+\\.\\d+)");
    
    public String getVersion() {
        try {
            InputStream is = null;
            Scanner scanner = null;
            try {
                is = this.getClass().getClassLoader().getResourceAsStream(METAINF_LOCATION);
                scanner = new Scanner(is);
                while (scanner.hasNext()) {
                    String line = scanner.nextLine();
                    if (line.startsWith(VERSION_PREFIX)) {
                        Matcher matcher = VERSION_PATTERN.matcher(line);
                        if (matcher.find()) {
                            String group = matcher.group();
                            return group;
                        }
                    }
                }
            } finally {
                if (scanner != null) {
                    scanner.close();
                }
                if (is != null) {
                    is.close();
                }
            }
        } catch (Exception e) {
            // TODO: log error!
        }
        return null;
    }
}

2. Register bean in faces-config.xml

    <managed-bean>
        <managed-bean-name>PropertiesBean</managed-bean-name>
        <managed-bean-class>com.someproject.presentation.PropertiesBean</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
    </managed-bean>

3. Use it on JSF page:

<h:outputText value="[v#{PropertiesBean.version}]" rendered="#{PropertiesBean.version != null}" />

Profit!

See also, http://stackoverflow.com/a/4782124

C#

Read and write files with stream in C#

fsutil
Very basic example how to read and write files. The util class is as following:

using System.IO;
using System.Text;

namespace Project.Dao
{
    public class FileSystemUtils
    {
        public static string ReadFile(string filePath, Encoding encoding)
        {
            using (var reader = new StreamReader(filePath, encoding))
            {
                return reader.ReadToEnd();
            }
        }

        public static void WriteFile(string filePath, string content, Encoding encoding)
        {
            using (var writer = new StreamWriter(filePath, false, encoding))
            {
                writer.Write(content);
            }
        }
    }
}

KISS paradigm enjoys.

Read and write text file in Java

Short for text file ex.

// Reading...
StringBuffer sb = new StringBuffer();
FileInputStream fis = null;
Reader reader = null;
try
{
	fis = new FileInputStream("in.txt");
	reader = new BufferedReader(new InputStreamReader(fis,"UTF-8"));
	char[] buffer = new char[1024];
	int read;
	while ((read = reader.read(buffer,0,buffer.length)) > 0)
		sb.append(buffer,0,read);
}
finally
{
	if (reader != null)
		reader.close();
	if (fis != null)
		fis.close();
}

// Processing...
String res = sb.toString() + "...OK!";

// Writing...
FileOutputStream fos = null;
Writer writer = null;
try
{
	fos = new FileOutputStream("out.txt");
	writer = new BufferedWriter(new OutputStreamWriter(fos,"UTF-8"));
	writer.write(res);
}
finally
{
	if (writer != null)
		writer.close();
	if (fos != null)
		fos.close();
}

Greate for me.

Read text file with Scanner in Java


Reading file with encoding.
Snipet:

File file = new File("filePath.txt");
Scanner scanner = new Scanner(new FileInputStream(file), "UTF-8");
try
{
	while (scanner.hasNextLine())
		System.out.println(scanner.nextLine());
}
finally
{
	scanner.close();
}