Recent Notes
Displaying keyword search results 1 - 10
Created by lokf on January 13, 2012 14:10:42
Last update: January 13, 2012 14:10:42
For some reason I don't know writing to files in Android is very complicated and tedious. Here is some code for those who might need it. Apps should write in the SD in the directory /Android/data/package_name/files/ so that it is deleted with the app uninstall. package randomname; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.nio.ByteBuffer; import android.content.Context; import android.os.Environment; import android.util.Log; public class FileIOLibrary { String packageName; boolean mExternalStorageAvailable = false; boolean mExternalStorageWriteable = false; FileIOLibrary(String packageNamep) { this.packageName=packageNamep; } public boolean isExternalStorageAvailable() { updateExternalStorageState(); return mExternalStorageAvailable; } public boolean isExternalStorageWritable() { updateExternalStorageState(); return mExternalStorageWriteable; } /** * writes the current state of SD card to the corresponding variables */ void updateExternalStorageState() {...
Created by Fang on November 08, 2011 20:55:00
Last update: November 21, 2011 18:19:44
In the simple taglib example , I used a tag handler class to implement a taglib. This is an example to implement a taglib with a UI component. The purpose is to use a custom tag to split a string and print each part in a separate paragraph, i.e., print
<p>john</p> <p>steve</p> <p>mike</p> with custom tag <my:foreach> : <my:foreach var="who" value="john steve mike"> ... These are the files: pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0"... src/main/java/com/example/UIForeash.java : package com.example; import java.io.IOExcep... src/main/resources/META-INF/faces-config.xml : <?xml version="1.0" encoding="UTF-8"?> <faces-c... src/main/resources/META-INF/foreach.taglib.xml : <?xml version="1.0" encoding="UTF-8"?> <facelet... How to use: Put the JAR file generated by the above project in the WEB-INF/lib folder of the web app. If the web app is a Maven project, just add the taglib project as a dependency:...
Created by Dr. Xi on November 11, 2011 10:05:22
Last update: November 11, 2011 10:12:01
This is an HTML image tag filter using Java regex. It takes a string, finds the img tags, replaces the src attribute with one provided by the filter, then adds a class name to the class attribute.
import java.util.regex.*;
import java.io.*;
...
Test file:
<div id="HTML snippet">
<img src="img/big/txt-m...
Created by magnum on October 20, 2011 20:44:23
Last update: October 20, 2011 20:53:26
Copied verbatim from The GNU C Library Manual . When you have finished using a socket, you can simply close its file descriptor with close ; see Opening and Closing Files . If there is still data waiting to be transmitted over the connection, normally close tries to complete this transmission. You can control this behavior using the SO_LINGER socket option to specify a timeout period; see Socket Options . You can also shut down only reception or transmission on a connection by calling shutdown , which is declared in sys/socket.h . Function: int shutdown (int socket, int how) The shutdown function shuts down the connection of socket socket. The argument how specifies what action to perform: 0 - Stop receiving data for this socket....
Created by freyo on May 13, 2011 15:45:29
Last update: September 20, 2011 08:08:12
This is an Android app that dumps any binarized xml file as plain text - to the sdcard on the device or emulator.
build.xml :
<?xml version="1.0" encoding="UTF-8"?>
<project...
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<man...
res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<Lin...
res/values/strings.xml :
<?xml version="1.0" encoding="utf-8"?>
<res...
src/com/android/xmltool/DumpXml.java
package com.android.xmltool;
import java.ut...
Screenshot
Pre-built APK can be downloaded from: http://code.google.com/p/android-binxml-dump/
Created by voodoo on August 12, 2011 14:43:57
Last update: August 14, 2011 15:19:21
#include <stdio.h>
#include <stdlib.h>
i...
Created by voodoo on August 12, 2011 14:44:45
Last update: August 12, 2011 14:44:45
#include <fstream>
#include <iostream>
i...
Created by voodoo on August 12, 2011 12:47:50
Last update: August 12, 2011 13:03:00
Use fstream and seekg :
#include <iostream>
#include <fstream>
i...
With stat :
#include <iostream>
#include <sys/stat.h>
...
Created by alfa on July 15, 2011 13:25:45
Last update: July 15, 2011 13:25:45
Read the whole contents of a file into a String. It's better to read the whole file as bytes and convert to String than to read the file line by line and concatenate the lines.
String getFileContents(String fileName) throws...
Using java.nio :
import java.io.FileInputStream;
import java...
Created by alfa on May 04, 2011 12:08:57
Last update: July 15, 2011 12:31:41
To read a whole file at once into a byte array:
File theFile = new File("test.bin");
byte[] byt...
It is important that the total number of bytes read is used for the while condition. Changing the loop to the following may result in an infinite loop !
int m = 0, n = 0;
while (n >= 0) {
n = i...