Click here to Skip to main content
15,885,537 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a file : language.txt use internal storage. I want to read file to a string and check this string to choose database.

This is my code:
Java
public class DBDAO {
	protected SQLiteDatabase database;
	private DatabaseHelper dbHelper;
	private DataBaseHelper_FR dbHelper_FR;
	private Context mContext;
public DBDAO(Context context) {
        this.mContext = context;
        dbHelper = new DatabaseHelper(mContext);
        open();
    }

public void open() throws SQLException {

		if(fileExists(mContext, "language.txt"))
		{
			lang = main.ReadFile();
		}
		else
		{
			main.WriteFile("English");
			lang = "English";
		}
		if (lang == "English") {
			if (dbHelper == null)
				dbHelper = new DatabaseHelper(mContext);
			database = dbHelper.getWritableDatabase();
		}

		else if (lang == "France") {
			if (dbHelper_FR == null)
				dbHelper_FR = new DataBaseHelper_FR(mContext);
			database = dbHelper_FR.getWritableDatabase();
		}
	}
	public boolean fileExists(Context context, String filename) {
		File file = context.getFileStreamPath(filename);
		if (file == null || !file.exists()) {
			return false;
		}
		return true;
	}
	
	// write text to file
		public void WriteFile(String writeString) {
			// add-write text into file
			try {
				FileOutputStream fileout = mContext.openFileOutput("language.txt", mContext.MODE_PRIVATE);
				OutputStreamWriter outputWriter = new OutputStreamWriter(fileout);
				outputWriter.write(writeString);
				outputWriter.close();

			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		// Read text from file
		public String ReadFile() {
			// reading text from file
			String s = "";
			try {
				FileInputStream fileIn = mContext.openFileInput("language.txt");
				InputStreamReader InputRead = new InputStreamReader(fileIn);
				char[] inputBuffer = new char[100];
				int charRead;
				while ((charRead = InputRead.read(inputBuffer)) > 0) {
					// char to string conversion
					String readstring = String
							.copyValueOf(inputBuffer, 0, charRead);
					s += readstring;
				}
				InputRead.close();

			} catch (Exception e) {
				e.printStackTrace();
			}
			return s;
		}
}


Where is my faults?
Thank so much.
Posted
Comments
Style-7 27-Apr-15 16:06pm    
Read file from asset project.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900