Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9049167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T12:19:35+00:00 2026-06-16T12:19:35+00:00

For my application I want to create a class where I can log data

  • 0

For my application I want to create a class where I can log data into a file. Within my app there’s the ability to email me if they’re experiencing a problem and data from this file could potentially help me dissect the issue. Firstly, am I going about this the wrong way? Is there a better way to log exceptions that occur?

The problem is if I attempt to use the log method from another class using:

Logger.log(0,"","","");

It fails to find the file or indeed create the file if it isn’t already created. The code is attached below.

package com.example.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.text.format.Time;
import android.util.Log;

public class Logger extends Activity {

final static String FileName = "Log";
static FileOutputStream fos;
static FileInputStream fis;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d("logger", "oncreate");
    try {
        File f = getFileStreamPath(FileName);
        if (!f.exists()) {
            Log.d("logger", "file doesn't exist");
            fos = openFileOutput(FileName, Context.MODE_APPEND);
            fos.write(("Created on " + Build.TIME + "\nDevice name: "
                    + Build.MODEL + " \nAndroid Version" + Build.VERSION.SDK_INT)
                    .getBytes());
            fos.close();
        }
        fos = openFileOutput(FileName, Context.MODE_APPEND);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

public static void log(int type, String TAG, String msg) {
    Log.d("logger", "file being written");
    Log.println(type, TAG, msg);
    Time now = new Time();
    now.setToNow();
    try {
        fos = new FileOutputStream(FileName);
        fos.write(("\n" + now.toString() + " " + type + " " + TAG + " " + msg).getBytes());
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T12:19:39+00:00Added an answer on June 16, 2026 at 12:19 pm

    The file name is not suffice to write a file. You need to give a directory when using FileOutputStream unlike when using openFileOutput. Thus if you change this line:

    final static String FileName = "Log";
    

    To

    final static String FileName = "data/data/com.example.test/files/Log";
    

    This will fix the issue. Finally oncreate is not called when accessing a method within a class. You have to combine the two as follows:

    final static String FileName = "data/data/com.example.test/files/Log";
    static FileOutputStream fos;
    
    public static void log(int priority, String tag, String msg) {
        Log.println(priority, tag, msg);
        try {
            Time now = new Time();
            now.setToNow();
            File f = new File(FileName);
            if (!f.exists()) {
                Log.i("Logger", "Creating Log file");
                fos = new FileOutputStream(f, true);
                fos.write(("Created on " + now.toString().subSequence(0, 15)
                        + "\nDevice name: " + Build.MODEL
                        + " \nAndroid Version " + Build.VERSION.SDK_INT)
                        .getBytes());
                fos.close();
            }
            fos = new FileOutputStream(f, true);
            Log.d("file", now.toString());
            fos.write(("\n" + now.toString().substring(4, 15) + " " + priority
                    + " " + tag + " " + msg).getBytes());
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want create an application with animate button? how can i do? after click
I want to create application which would display spatial data on Map. I'm thinking
I want to write data to a text file. But, in my application, i
I want to create a Java Desktop Application and want to employ some mechanism
In my application I want to create a dialog which has a button to
in my silverlight application i want to create a style from code behind and
For an MVC3 application I want to create a reusable DAL that is accessed
I have a Homework Planner application which I want to create an Alarm for
I'm working on a ASP.NET MVC3 web application and I want to create a
I want to create an application with JayData + WCF/RIA Services but i need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.