Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

93
LINES

< > BotCompany Repo | #1000411 // Testing file I/O on Android

JavaX source code (Android) - run with: the app

1  
import android.widget.*;
2  
import android.view.*;
3  
import android.content.Context;
4  
import android.app.Activity;
5  
import java.util.Timer;
6  
import java.util.TimerTask;
7  
import java.io.*;
8  
9  
public class main {
10  
  static class Lg {
11  
    Activity context;
12  
    ScrollView sv;
13  
    TextView tv;
14  
    StringBuilder buf = new StringBuilder();
15  
    
16  
    Lg(Activity context) {
17  
      this.context = context;
18  
      sv = new ScrollView(context);
19  
      tv = new TextView(context);
20  
      tv.setText(buf.toString());
21  
      sv.addView(tv);
22  
    }
23  
    
24  
    View getView() {
25  
      return sv;
26  
    }
27  
    
28  
    void print(final String s) {
29  
      context.runOnUiThread(new Runnable() {
30  
        public void run() {
31  
          buf.append(s);
32  
          tv.setText(buf.toString());
33  
        }
34  
      });
35  
    }
36  
    
37  
    void println(String s) {
38  
      print(s + "\n");
39  
    }
40  
  }
41  
  
42  
  static Lg lg;
43  
  
44  
  public static View main(final Activity context) {
45  
    lg = new Lg(context);
46  
    
47  
    OutputStream outputStream = new OutputStream() {
48  
      public void write(int b) {
49  
        try {
50  
          lg.print(new String(new byte[] {(byte) b}, "UTF-8")); // This is crap
51  
        } catch (UnsupportedEncodingException e) {}
52  
      }
53  
      
54  
      @Override
55  
      public void write(byte[] b, int off, int len) {
56  
        try {
57  
          lg.print(new String(b, off, len, "UTF-8")); // This is crap
58  
        } catch (UnsupportedEncodingException e) {}
59  
      }
60  
    };
61  
    
62  
    PrintStream ps = new PrintStream(outputStream, true);
63  
    System.setOut(ps);
64  
    System.setErr(ps);
65  
    
66  
    new Thread() {
67  
      public void run() {
68  
        try {
69  
          String file = "input.txt";
70  
          // Fails - "read-only file system, /input.txt"
71  
          // FileOutputStream out = new FileOutputStream(file);
72  
          FileOutputStream out = context.openFileOutput(file, Context.MODE_PRIVATE);
73  
74  
          PrintStream ps = new PrintStream(out);
75  
          ps.println("hello");
76  
          ps.close();
77  
          out.close();
78  
          System.out.println("Wrote to " + file);
79  
          
80  
          //FileInputStream in = new FileInputStream(file);
81  
          FileInputStream in = context.openFileInput(file);
82  
          int b = in.read();
83  
          in.close();
84  
          System.out.println("Read from " + file + ": " + (char) b);
85  
        } catch (Throwable e) {
86  
          e.printStackTrace();
87  
        }
88  
      }
89  
    }.start();
90  
    
91  
    return lg.getView();
92  
  }
93  
}

Author comment

Began life as a copy of #1000406

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1000411
Snippet name: Testing file I/O on Android
Eternal ID of this version: #1000411/1
Text MD5: 7c2258926be61a79868e250091d1d321
Author: stefan
Category: javax android
Type: JavaX source code (Android)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-08-03 15:14:16
Source code size: 2474 bytes / 93 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 628 / 486
Referenced in: [show references]