Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jdt-dev] Reading/Writing UTF-8 fromat

M. Bashir Al-Noimi wrote:
> Firstly, I'm not sure that this message in its correct place ... so I'm
> sorry if there is a mistake .
>
> i need to read and write UTF8 text format but i couldn't do that by
> using this code (reading process is correct but writing is wrong)... can
> you help me.
>
> remark :
> if the UTF8 file was short writing process done well, but if it was big writing process not working .
i fixed this problem by modifying my code as shown in the attached file
... thanks

--
Best Regards تحياتي
Muhammad Bashir Al-Noimi محمد بشير النعيمي

مدونتي My Blog
http://www.hali-sy.net/bashir

package src;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;


public class Po2Tab {
	
	private List lst;
	private String Path;
	private StringBuffer Buffer;
	private static final String SYS_LINE = System.getProperty("line.separator");
	private static final String SYS_SPARATOR = System.getProperty("file.separator");
	
	public Po2Tab(String PoFilePath){
		Path = PoFilePath;
	}
	
	public String getString() {
//		return lst.toString().replace(",", "");
		return Buffer.toString();
	}
	

	public List getList() {
		return lst;
	}
	
	public void OpenFromFile() {
		try {
			BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(Path), "UTF8"));
			Buffer = new StringBuffer();
			lst = new ArrayList();
			String temp;
			int x = 0;
			while( ( temp = in.readLine()) != null) {
				if (temp.contains("msgid \"")) {
					x++;
					System.out.println("Adding word Nr.:"+String.valueOf(x));
					temp = temp.toLowerCase();
					temp = temp.replace("msgid \"", "");
					temp = temp.replace("\"", "");
					temp = temp.trim();
					temp += "\t";
					temp.replace("  ", "\t");
				}else if(temp.contains("msgstr \"")){
					x++;
					if (!temp.contains("\t")) {
						temp+="\t";
					}
					temp = temp.toLowerCase();
					temp = temp.replace("msgstr \"", "");
					temp = temp.replace("::", "\\n");
					temp = temp.replace("،", "\\n");
					temp = temp.replace("\"", "");
					temp = temp.trim();
					temp = temp.replace(" \\n ", "\\n");
					temp.replace("  ", "\t");
					temp += "\n";
				}
				else{
					temp="";
				}	
				if(temp != "")
				{
					lst.add(temp);
					Buffer.append(temp);
				}
			}
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	public void OpenFromFile(String Path){
		try {
			BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(Path), "UTF8"));
			Buffer = new StringBuffer();
			lst = new ArrayList();
			String temp;
			int x = 0;
			while( ( temp = in.readLine()) != null) {
				if (temp.contains("msgid \"")) {
					x++;
					System.out.println("Adding word Nr.:"+String.valueOf(x));
					temp = temp.toLowerCase();
					temp = temp.replace("msgid \"", "");
					temp = temp.replace("\"", "");
					temp = temp.trim();
					temp += "\t";
					temp.replace("  ", "\t");
				}else if(temp.contains("msgstr \"")){
					x++;
					if (!temp.contains("\t")) {
						temp+="\t";
					}
					temp = temp.toLowerCase();
					temp = temp.replace("msgstr \"", "");
					temp = temp.replace("::", "\\n");
					temp = temp.replace("،", "\\n");
					temp = temp.replace("\"", "");
					temp = temp.trim();
					temp = temp.replace(" \\n ", "\\n");
					temp.replace("  ", "\t");
					temp += "\n";
				}
				else{
					temp="";
				}	
				if(temp != "")
				{
					lst.add(temp);
					Buffer.append(temp);
				}
			}
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void SaveToFile() {
		try {
			PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(Path.replace(".po", ".tab")), "UTF8")));
			out.write(getString());
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void SaveToFile(String Path){
		try {
			PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(Path), "UTF8")));
			out.write(getString());
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public int getFileLenght() {
		return lst.size();
	}
	
}

Back to the top