Am using following code to read last four lines from the file but return first line to null why? How to solve this problem? Please help me?
public void read(){
StringBuilder text = new StringBuilder();
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath()+File.separator+"GPS");
dir.mkdirs();
String fname = "gps.txt";
File file = new File (dir, fname);
String[] last4 = new String[4];
int count=0;
try {
BufferedReader br = new BufferedReader(new FileReader(file));
while(br.ready()){
last4[count++%4]=br.readLine();
}
for (int i=0; i<4;i++){
text.append(last4[(i+count)%4]);
text.append('\n');
}
br.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
You have to check if file exists. According to your code copy paste this,
public void read() throws FileNotFoundException{
StringBuilder text = new StringBuilder();
String fname = "asdf.txt";
String path = Environment.getExternalStorageDirectory()+File.separator+"GPS"+File.separator+fname;
//You have to check if file exists
File file = new File(path);
if(!file.exists()){
//TODO do smth if your file doesnt exist
return;
}
BufferedReader br = null;
String[] last4 = new String[4];
int count=0;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader(path));
while ((sCurrentLine = br.readLine()) != null ) {
String str = sCurrentLine;
last4[count%4] = str;
count++;
}
for (int i=0; i<4;i++){
text.append(last4[i]);
text.append('\n');
}
System.out.println(text.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
Try This.
private static BufferedReader innerReader;
private static BufferedReader innerReader1;
private static final int UNTIL_LINE = 4;
public static int countLine(Reader reader) throws IllegalArgumentException
{
int countLine = 0;
if(reader == null)
{
throw new IllegalArgumentException("Null reader");
}
String line;
innerReader1 = new BufferedReader(reader);
try
{
while((line = innerReader1.readLine()) != null)
{
countLine++;
}
}catch(IOException e){}
return countLine;
}
public static List<String> loadFile(Reader reader, int countLine)
throws IllegalArgumentException{
List<String> fileList = new ArrayList<String>();
if(reader == null)
{
throw new IllegalArgumentException("Null Reader");
}
String line;
int thisLine = 0;
innerReader = new BufferedReader(reader);
try
{
while((line = innerReader.readLine()) != null)
{
if (line == null || line.trim().isEmpty())
throw new IllegalArgumentException(
"Line Empty");
thisLine++;
if(thisLine > countLine-UNTIL_LINE)
{
fileList.add(line);
}
}
} catch (IOException e) {
}
return fileList;
}
To test code.
int lines = 0;
List<String> test = new ArrayList<String>();
try {
lines = countLine(new FileReader("YourFile.txt"));
} catch (IOException e1) {
e1.printStackTrace();
}
try {
test = loadFile(new FileReader("YourFile.txt"), lines);
} catch (IOException e) {
e.printStackTrace();
}
for(String s : test)
{
System.out.println(s);
}
Full file example:
1 Line
2 Line
3 Line
4 Line
5 Line
6 Line
Result:
3 Line
4 Line
5 Line
6 Line
Are you sure the file exists and you have read access? It is odd that you are creating the parent directory before reading. Check your logcat for the stacktrace. Also, make sure you declared the "android.permission.WRITE_EXTERNAL_STORAGE" permission in your AndroidManifest.
I wrote the following method real quick. Should work fine for you.
public static String[] tail(File file, int tail) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(file));
List<String> lines = new ArrayList<>();
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
lines.add(line);
}
reader.close();
return lines.subList(Math.max(0, lines.size() - tail), lines.size()).toArray(new String[tail]);
}
Example usage:
try {
File gps = new File(Environment.getExternalStorageDirectory(), "GPS/gps.txt");
String[] lastFourLines = tail(gps, 4);
} catch (IOException e) {
// Are you sure the file exists?
// Did you declare "android.permission.WRITE_EXTERNAL_STORAGE" permission?
}
I have been working on this for a while and I am about to pull my hair out!!
If I use this...
public void readFile() {
BufferedReader buffReader = null;
StringBuilder result = new StringBuilder();
try {
FileInputStream fileIn = openFileInput("VariableStore.txt");
buffReader = new BufferedReader(new InputStreamReader(fileIn));
String line;
while ((line = buffReader.readLine()) != null) {
result.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
assert buffReader != null;
buffReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
String resultString = result.toString();
String[] controlString = resultString.split("$");
// String wb = controlString[4];
// String sb = controlString[5];
((Button) this.findViewById(R.id.wakeButton)).setText(resultString);
// ((Button) this.findViewById(R.id.sleepButton)).setText(sb);
// ((Button)this.findViewById(R.id.wakeButton)).setText(result);
// ((Button)this.findViewById(R.id.wakeButton)).setText(result);
// ((Button)this.findViewById(R.id.wakeButton)).setText(result);
}
The Button.setText works fine with "resultString" or with "result" which is a string I have input formatted as xxx$xxx$xxx$xxx$xxx so when I read it back in with the readFile() I want to use .Split and put it into an array "controlString" and then assign the array elements to my widgets i.e. setText(controlString[0]); but if I so much as even uncomment the lines String wb = controlString[4]; or String sb = controlString[5]; my program crashes. Why wont the array elemts work here?
Here is my writeFile().... (Which works perfectly.
public void writeFile() {
BufferedWriter buffWriter = null;
String wb = ((Button)this.findViewById(R.id.wakeButton)).getText().toString();
String sb = ((Button)this.findViewById(R.id.sleepButton)).getText().toString();
String tb = ((EditText)this.findViewById(R.id.textHoursBetween)).getText().toString();
String ti = ((EditText)this.findViewById(R.id.textIncrementTime)).getText().toString();
String td = ((EditText)this.findViewById(R.id.textIncrementDays)).getText().toString();
String writeString = wb + "$" + sb + "$" + tb + "$" + ti + "$" + td;
try {
FileOutputStream fileOut = openFileOutput("VariableStore.txt", Context.MODE_PRIVATE);
buffWriter = new BufferedWriter(new OutputStreamWriter(fileOut));
try {
buffWriter.write(writeString);
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally {
try {
assert buffWriter != null;
buffWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
I found the problem...
Instead of this:
String[] controlString = resultString.split("$");
I had to use this:
String[] controlString = resultString.split(Pattern.quote("$"));
public class Utils {
public static List<Message> getMessages() {
//File file = new File("file:///android_asset/helloworld.txt");
AssetManager assetManager = getAssets();
InputStream ims = assetManager.open("helloworld.txt");
}
}
I am using this code trying to read a file from assets. I tried two ways to do this. First, when use File I received FileNotFoundException, when using AssetManager getAssets() method isn't recognized.
Is there any solution here?
Here is what I do in an activity for buffered reading extend/modify to match your needs
BufferedReader reader = null;
try {
reader = new BufferedReader(
new InputStreamReader(getAssets().open("filename.txt")));
// do reading, usually loop until end of file reading
String mLine;
while ((mLine = reader.readLine()) != null) {
//process line
...
}
} catch (IOException e) {
//log the exception
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
//log the exception
}
}
}
EDIT : My answer is perhaps useless if your question is on how to do it outside of an activity. If your question is simply how to read a file from asset then the answer is above.
UPDATE :
To open a file specifying the type simply add the type in the InputStreamReader call as follow.
BufferedReader reader = null;
try {
reader = new BufferedReader(
new InputStreamReader(getAssets().open("filename.txt"), "UTF-8"));
// do reading, usually loop until end of file reading
String mLine;
while ((mLine = reader.readLine()) != null) {
//process line
...
}
} catch (IOException e) {
//log the exception
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
//log the exception
}
}
}
EDIT
As #Stan says in the comment, the code I am giving is not summing up lines. mLine is replaced every pass. That's why I wrote //process line. I assume the file contains some sort of data (i.e a contact list) and each line should be processed separately.
In case you simply want to load the file without any kind of processing you will have to sum up mLine at each pass using StringBuilder() and appending each pass.
ANOTHER EDIT
According to the comment of #Vincent I added the finally block.
Also note that in Java 7 and upper you can use try-with-resources to use the AutoCloseable and Closeable features of recent Java.
CONTEXT
In a comment #LunarWatcher points out that getAssets() is a class in context. So, if you call it outside of an activity you need to refer to it and pass the context instance to the activity.
ContextInstance.getAssets();
This is explained in the answer of #Maneesh. So if this is useful to you upvote his answer because that's him who pointed that out.
getAssets()
is only works in Activity in other any class you have to use Context for it.
Make a constructor for Utils class pass reference of activity (ugly way) or context of application as a parameter to it. Using that use getAsset() in your Utils class.
Better late than never.
I had difficulties reading files line by line in some circumstances.
The method below is the best I found, so far, and I recommend it.
Usage: String yourData = LoadData("YourDataFile.txt");
Where YourDataFile.txt is assumed to reside in assets/
public String LoadData(String inFile) {
String tContents = "";
try {
InputStream stream = getAssets().open(inFile);
int size = stream.available();
byte[] buffer = new byte[size];
stream.read(buffer);
stream.close();
tContents = new String(buffer);
} catch (IOException e) {
// Handle exceptions here
}
return tContents;
}
public String ReadFromfile(String fileName, Context context) {
StringBuilder returnString = new StringBuilder();
InputStream fIn = null;
InputStreamReader isr = null;
BufferedReader input = null;
try {
fIn = context.getResources().getAssets()
.open(fileName, Context.MODE_WORLD_READABLE);
isr = new InputStreamReader(fIn);
input = new BufferedReader(isr);
String line = "";
while ((line = input.readLine()) != null) {
returnString.append(line);
}
} catch (Exception e) {
e.getMessage();
} finally {
try {
if (isr != null)
isr.close();
if (fIn != null)
fIn.close();
if (input != null)
input.close();
} catch (Exception e2) {
e2.getMessage();
}
}
return returnString.toString();
}
one line solution for kotlin:
fun readFileText(fileName: String): String {
return assets.open(fileName).bufferedReader().use { it.readText() }
}
Also you can use it as extension function everyWhere
fun Context.readTextFromAsset(fileName : String) : String{
return assets.open(fileName).bufferedReader().use {
it.readText()}
}
Simply call in any context Class
context.readTextFromAsset("my file name")
AssetManager assetManager = getAssets();
InputStream inputStream = null;
try {
inputStream = assetManager.open("helloworld.txt");
}
catch (IOException e){
Log.e("message: ",e.getMessage());
}
getAssets() method will work when you are calling inside the Activity class.
If you calling this method in non-Activity class then you need to call this method from Context which is passed from Activity class. So below is the line by you can access the method.
ContextInstance.getAssets();
ContextInstance may be passed as this of Activity class.
Reading and writing files have always been verbose and error-prone. Avoid these answers and just use Okio instead:
public void readLines(File file) throws IOException {
try (BufferedSource source = Okio.buffer(Okio.source(file))) {
for (String line; (line = source.readUtf8Line()) != null; ) {
if (line.contains("square")) {
System.out.println(line);
}
}
}
}
Here is a method to read a file in assets:
/**
* Reads the text of an asset. Should not be run on the UI thread.
*
* #param mgr
* The {#link AssetManager} obtained via {#link Context#getAssets()}
* #param path
* The path to the asset.
* #return The plain text of the asset
*/
public static String readAsset(AssetManager mgr, String path) {
String contents = "";
InputStream is = null;
BufferedReader reader = null;
try {
is = mgr.open(path);
reader = new BufferedReader(new InputStreamReader(is));
contents = reader.readLine();
String line = null;
while ((line = reader.readLine()) != null) {
contents += '\n' + line;
}
} catch (final Exception e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException ignored) {
}
}
if (reader != null) {
try {
reader.close();
} catch (IOException ignored) {
}
}
}
return contents;
}
You can load the content from the file. Consider the file is present in asset folder.
public static InputStream loadInputStreamFromAssetFile(Context context, String fileName){
AssetManager am = context.getAssets();
try {
InputStream is = am.open(fileName);
return is;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static String loadContentFromFile(Context context, String path){
String content = null;
try {
InputStream is = loadInputStreamFromAssetFile(context, path);
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
content = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return content;
}
Now you can get the content by calling the function as follow
String json= FileUtil.loadContentFromFile(context, "data.json");
Considering the data.json is stored at Application\app\src\main\assets\data.json
In MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tvView = (TextView) findViewById(R.id.tvView);
AssetsReader assetsReader = new AssetsReader(this);
if(assetsReader.getTxtFile(your_file_title)) != null)
{
tvView.setText(assetsReader.getTxtFile(your_file_title)));
}
}
Also, you can create separate class that does all the work
public class AssetsReader implements Readable{
private static final String TAG = "AssetsReader";
private AssetManager mAssetManager;
private Activity mActivity;
public AssetsReader(Activity activity) {
this.mActivity = activity;
mAssetManager = mActivity.getAssets();
}
#Override
public String getTxtFile(String fileName)
{
BufferedReader reader = null;
InputStream inputStream = null;
StringBuilder builder = new StringBuilder();
try{
inputStream = mAssetManager.open(fileName);
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while((line = reader.readLine()) != null)
{
Log.i(TAG, line);
builder.append(line);
builder.append("\n");
}
} catch (IOException ioe){
ioe.printStackTrace();
} finally {
if(inputStream != null)
{
try {
inputStream.close();
} catch (IOException ioe){
ioe.printStackTrace();
}
}
if(reader != null)
{
try {
reader.close();
} catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
Log.i(TAG, "builder.toString(): " + builder.toString());
return builder.toString();
}
}
In my opinion it's better to create an interface, but it's not neccessary
public interface Readable {
/**
* Reads txt file from assets
* #param fileName
* #return string
*/
String getTxtFile(String fileName);
}
Here is a way to get an InputStream for a file in the assets folder without a Context, Activity, Fragment or Application. How you get the data from that InputStream is up to you. There are plenty of suggestions for that in other answers here.
Kotlin
val inputStream = ClassLoader::class.java.classLoader?.getResourceAsStream("assets/your_file.ext")
Java
InputStream inputStream = ClassLoader.class.getClassLoader().getResourceAsStream("assets/your_file.ext");
All bets are off if a custom ClassLoader is in play.
ExceptionProof
It maybe too late but for the sake of others who look for the peachy answers.
loadAssetFile() method returns the plain text of the asset, or defaultValue argument if anything goes wrong.
public static String loadAssetFile(Context context, String fileName, String defaultValue) {
String result=defaultValue;
InputStreamReader inputStream=null;
BufferedReader bufferedReader=null;
try {
inputStream = new InputStreamReader(context.getAssets().open(fileName));
bufferedReader = new BufferedReader(inputStream);
StringBuilder out= new StringBuilder();
String line = bufferedReader.readLine();
while (line != null) {
out.append(line);
line = bufferedReader.readLine();
}
result=out.toString();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
Objects.requireNonNull(inputStream).close();
} catch (Exception e) {
e.printStackTrace();
}
try {
Objects.requireNonNull(bufferedReader).close();
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
If you use other any class other than Activity, you might want to do like,
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader( YourApplication.getInstance().getAssets().open("text.txt"), "UTF-8"));
Using Kotlin, you can do the following to read a file from assets in Android:
try {
val inputStream:InputStream = assets.open("helloworld.txt")
val inputString = inputStream.bufferedReader().use{it.readText()}
Log.d(TAG,inputString)
} catch (e:Exception){
Log.d(TAG, e.toString())
}
cityfile.txt
public void getCityStateFromLocal() {
AssetManager am = getAssets();
InputStream inputStream = null;
try {
inputStream = am.open("city_state.txt");
} catch (IOException e) {
e.printStackTrace();
}
ObjectMapper mapper = new ObjectMapper();
Map<String, String[]> map = new HashMap<String, String[]>();
try {
map = mapper.readValue(getStringFromInputStream(inputStream), new TypeReference<Map<String, String[]>>() {
});
} catch (IOException e) {
e.printStackTrace();
}
ConstantValues.arrayListStateName.clear();
ConstantValues.arrayListCityByState.clear();
if (map.size() > 0)
{
for (Map.Entry<String, String[]> e : map.entrySet()) {
CityByState cityByState = new CityByState();
String key = e.getKey();
String[] value = e.getValue();
ArrayList<String> s = new ArrayList<String>(Arrays.asList(value));
ConstantValues.arrayListStateName.add(key);
s.add(0,"Select City");
cityByState.addValue(s);
ConstantValues.arrayListCityByState.add(cityByState);
}
}
ConstantValues.arrayListStateName.add(0,"Select States");
}
// Convert InputStream to String
public String getStringFromInputStream(InputStream is) {
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
String line;
try {
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sb + "";
}
The Scanner class may simplify this.
StringBuilder sb=new StringBuilder();
Scanner scanner=null;
try {
scanner=new Scanner(getAssets().open("text.txt"));
while(scanner.hasNextLine()){
sb.append(scanner.nextLine());
sb.append('\n');
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if(scanner!=null){try{scanner.close();}catch (Exception e){}}
}
mTextView.setText(sb.toString());
#HpTerm answer Kotlin version:
private fun getDataFromAssets(activity: Activity): String {
var bufferedReader: BufferedReader? = null
var data = ""
try {
bufferedReader = BufferedReader(
InputStreamReader(
activity?.assets?.open("Your_FILE.html"),
"UTF-8"
)
) //use assets? directly if inside the activity
var mLine:String? = bufferedReader.readLine()
while (mLine != null) {
data+= mLine
mLine=bufferedReader.readLine()
}
} catch (e: Exception) {
e.printStackTrace()
} finally {
try {
bufferedReader?.close()
} catch (e: Exception) {
e.printStackTrace()
}
}
return data
}
I am trying to make the computer read a text file full of words and add it to an ArrayList. I made it work on a regular Java application, but can't get it to work on Android. Can someone help me out?
try {
FileInputStream textfl = (FileInputStream) getAssets().open("test.txt");
DataInputStream is = new DataInputStream(textfl);
BufferedReader r = new BufferedReader(new InputStreamReader(is));
String strLine;
while ((strLine = r.readLine()) != null) {
tots.add(strLine); //tots is the array list
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I keep getting a error. The text file is 587kb, so could that be a problem?
try this.
private static String readTextFile(String fileName)
{
BufferedReader in = null;
try
{
in = new BufferedReader(new InputStreamReader(getAssets().open(fileName)));
String line;
final StringBuilder buffer = new StringBuilder();
while ((line = in.readLine()) != null)
{
buffer.append(line).append(System.getProperty("line.separator"));
}
return buffer.toString();
}
catch (final IOException e)
{
return "";
}
finally
{
try
{
in.close();
}
catch (IOException e)
{
// ignore //
}
}
}
i searched how to use resources under the directory "assets", then i find a snippet:
AssetManager assets = getAssets();
((TextView)findViewById(R.id.txAssets)).setText(**readStream**(assets.open("data.txt")));
i just cannot find what's the readStream method, it is not in the google apis
i tried to download the newest Java api document, but still can not find it, anybody knows that?
As #Felix said it is a user-defined method. On the page you linked, they defined readStream like this:
private String readStream(InputStream is) {
try {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
int i = is.read();
while(i != -1) {
bo.write(i);
i = is.read();
}
return bo.toString();
} catch (IOException e) {
return "";
}
}
This is better solution:
private String readStream(InputStream is) throws IOException {
StringBuilder sb = new StringBuilder();
BufferedReader r = new BufferedReader(new InputStreamReader(is),1000);
for (String line = r.readLine(); line != null; line =r.readLine()){
sb.append(line);
}
is.close();
return sb.toString();
}
It is much faster than ByteArrayOutputStream logic.
Agree with aviomaksim.
private static String readStream(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
Log.e(TAG, "IOException", e);
} finally {
try {
is.close();
} catch (IOException e) {
Log.e(TAG, "IOException", e);
}
}
return sb.toString();
}