3

I have a C structure with 2 attributes, say the content and the val How do I read this into an android file? Does android support any structure?

My structure is like

struct data[] = {
    {"aakash", 2260},
    {"anuj", 1}}

How to read such structures in android?

Aakash Anuj
  • 3,773
  • 7
  • 35
  • 47
  • @TrafalgarLaw I dont know much about JNI ? Can you please elaborate on how can i read C structure in android? – Aakash Anuj Nov 28 '12 at 06:09
  • This will help you. Please let me know any other issues. http://stackoverflow.com/questions/5771366/reading-a-simple-text-file – Akshay Joy Nov 28 '12 at 06:41

2 Answers2

1

For Android supports Java Platform. Structure is not belong to Java. You Can Achieve the Same with Pojo Classes.

Public Class MyData {

    private String username;
    private double points;

    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public double getPoints() {
            return points;
    }
    public void setPoints(double points) {
        this.points = points;
    }
    public MyData(String username,double points)
    {
        this.username = username;
        this.points = points; 
    }
}


MyData[] data = new MyData[2];

data[0]= new MyData("aakash", 2260);
data[1]= new MyData("anuj", 1);
Ofir Luzon
  • 10,635
  • 3
  • 41
  • 52
Akshay Joy
  • 1,765
  • 1
  • 14
  • 23
  • My database is very large..i cannot use classes for it...isn't it? – Aakash Anuj Nov 28 '12 at 06:21
  • Here there are two Fields, so you can go ahead with Pojo classes, as I explained. How come Database is related in this context. – Akshay Joy Nov 28 '12 at 06:28
  • I have my data in a file, so how to I read the file... The file is in the from "aakash" 12 \n "anuj" 6 \n and so on – Aakash Anuj Nov 28 '12 at 06:31
  • InputStream inputStream = getResources().openRawResource(R.raw.toc); // or you can use AssestManager to open File like this /* AssetManager am = context.getAssets(); InputStream is = am.open("test.txt"); */ System.out.println(inputStream); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); int i; try { i = inputStream.read(); while (i != -1) { byteArrayOutputStream.write(i); i = inputStream.read(); } inputStream.close(); } catch (IOException e) { e.printStackTrace(); } – Akshay Joy Nov 28 '12 at 06:36
0

If the structure is as simple as a String and an Integer, and you really wanna write to file using C, read using Java. You can use a Plain Text File, Use a CSV format to store the values. It'll be easy to read/write in both languages.

You can't store those objects into a binary file and have Java read it effortlessly. Although, If the file was created using Java, then it should be easy.

st0le
  • 33,375
  • 8
  • 89
  • 89