Android File I/O & Language

Android file writing using a variety of paradigms. Note it is best to add a BufferedReader buffer size – or the default is 8K.

package com.readwriter;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Random;

public class CheckFileForRepeats2 {
OutputStream outputStream;
static ArrayList ar = new ArrayList();
static ArrayList br = new ArrayList();
String endOfVerb, endOfVerb_1, theAnswer;
private static String x;
static String newy;
public static void main(String[] args) {
CheckFileForRepeats2 neu = new CheckFileForRepeats2();
for(int i = 2;i<92316;i++){ // i must be 1 more than lines in file
x = neu.readLines("RepeatsCheck.txt",i);
if(ar.contains(x)){
System.out.println(" contain " + x);
}
ar.add(neu.findWord("RepeatsCheck.txt", x));
}

} // end of main
public static void writeIt(String sentence_to_write, String file) throws Exception {
OutputStream outputStream;
outputStream = new FileOutputStream("res/raw/"+file,true);
Writer writer = new OutputStreamWriter(outputStream);
try {
writer.write(sentence_to_write + "\n");
} catch (IOException e) {
}
try {
writer.close();
} catch (IOException e) {
}
} // end of writeIt method
String findWord(String file, String theWord){ // machtes theWord to one in
Random generator = new Random();
int numy = generator.nextInt(f_tot) + 1;
InputStream in = this.getResources().openRawResource(R.raw.wordlist);
InputStream in;
try{
in = new FileInputStream("/"+file); // on the effing c drive
} catch(Exception e) {
try {
in = new FileInputStream("/"+file);
} catch(Exception e2){ return "didntread_1"; }
}
StringBuffer inLine = new StringBuffer();
InputStreamReader isr = new InputStreamReader(in);
BufferedReader inRd = new BufferedReader(isr);
String text = "";
String one_line = "";
try {
int county = 0;
while ((text = inRd.readLine()) != null) {
county += 1;
if (text.equals(theWord)){
one_line = text;
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
}
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
}
return one_line.toLowerCase().trim();
}

String getWord(String file, int f_tot){ // gets one random word
Random generator = new Random();
int numy = generator.nextInt(f_tot) + 1;
InputStream in;
try{
in = new FileInputStream("/"+file);
} catch(Exception e) {
try {
in = new FileInputStream("/"+file);
} catch(Exception e2){ return "didntread_1"; }
}
StringBuffer inLine = new StringBuffer();
InputStreamReader isr = new InputStreamReader(in);
BufferedReader inRd = new BufferedReader(isr);
String text = "";
String one_line = "";
try {
int county = 0;
while ((text = inRd.readLine()) != null) {
county += 1;
inLine.append(county + " " + text);
inLine.append("\n");
if (county==numy){
one_line = text + " " + String.valueOf(county);}
}
} catch (IOException e) {
// TODO Auto-generated catch block
}
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
}
return one_line.toLowerCase().trim();
}
String readLines(String file, int lineNumber){
;
InputStream in;
try{
in = new FileInputStream("/"+file); // on the effing c drive
} catch(Exception e) {
try {
in = new FileInputStream("/"+file);
} catch(Exception e2){ return "didntread_1"; }
}
StringBuffer inLine = new StringBuffer();
InputStreamReader isr = new InputStreamReader(in);
BufferedReader inRd = new BufferedReader(isr);
String text = "";
String one_line = "";
try {
int county = 0;
while ((text = inRd.readLine()) != null) {
county += 1;
if (lineNumber==county){
one_line = text;
}
}
} catch (IOException e) {
// TODO must handle this
}
try {
in.close();

} catch (IOException e) {
// TODO Auto-generated catch block
}
return one_line;
}

void writeIds(String ids) throws IOException{
try {
outputStream = new FileOutputStream("sdcard/Android/data/billtododo",true); // append is true
} catch (FileNotFoundException e) {
try{ // does not write 44 if it finds previous and writes
outputStream = new FileOutputStream("sdcard/billtododo");
} catch (IOException e2) { } }
Writer writer = new OutputStreamWriter(outputStream);
try {
writer.write("p"+ids);
} catch (IOException e) { }
writer.close();
}

private String readLines() throws FileNotFoundException{
String one_line2 = "";
InputStream in2;
try{
in2 = new FileInputStream("sdcard/Android/data/billtododo");
} catch(Exception e) {
try {
in2 = new FileInputStream("sdcard/billtododo");
} catch(Exception e2){ return "didntread_1"; }
}
InputStreamReader isr2 = new InputStreamReader(in2);
BufferedReader inRd2 = new BufferedReader(isr2);
String text2 = "";
try {
while ((text2 = inRd2.readLine()) != null) {
one_line2 = text2;
}
} catch (IOException e) {
return "didntread_3";
}
try {
in2.close();
} catch (IOException e) {
return "didntread_4";
}
return one_line2;
}

}

package com.umba;
command line :)
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;

public class MakeFileRegGerunds {
OutputStream outputStream;
static ArrayList ar = new ArrayList();
String endOfVerb, endOfVerb_1, theAnswer;
private static String x;

public static void main(String[] args) {
MakeFileRegGerunds ma = new MakeFileRegGerunds();
System.out.println(ma.pastParticiple(“taste”)); // debugging
for(int i = 1;i<621;i++){ // i must be 1 more than lines in file, go thro whole file
x = ma.readLines("MakeFileRegSimplePast.txt",i);
System.out.println(ma.gerund(x));
}

}
String readLines(String file, int lineNumber){
Random generator = new Random();
int numy = generator.nextInt(f_tot) + 1;
InputStream in = this.getResources().openRawResource(R.raw.wordlist);
InputStream in;
try{
in = new FileInputStream("/"+file); // on the effing c drive
} catch(Exception e) {
try {
in = new FileInputStream("/"+file);
} catch(Exception e2){ return "didntread_1"; }
}
StringBuffer inLine = new StringBuffer();
InputStreamReader isr = new InputStreamReader(in);
BufferedReader inRd = new BufferedReader(isr);
String text = "";
String one_line = "";
try {
int county = 0;
while ((text = inRd.readLine()) != null) {
county += 1;
if (lineNumber==county){
// if(!text.equals("")){ // takes care of lines with no word?
one_line = text;
// }
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
}
try {
in.close();
// textview02.setText(one_line.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
}
return one_line.toLowerCase().trim();
}

// begin gerunds from regular verb

private String gerund(String verb_infinitive){
endOfVerb = verb_infinitive.substring(verb_infinitive.length()-1); // need this in cases
endOfVerb_1 = verb_infinitive.substring(verb_infinitive.length()-2,verb_infinitive.length()-1);
if(endOfVerb.equals("e")){
theAnswer = verb_infinitive.substring(0, verb_infinitive.length()-1) + "ing";

} else {
theAnswer = verb_infinitive + "ing";
}

if(endOfVerb.equals("t")){
if(endOfVerb_1.equals("a") || endOfVerb_1.equals("e")|| endOfVerb_1.equals("i")|| endOfVerb_1.equals("o")|| endOfVerb_1.equals("u")){
theAnswer = verb_infinitive + "ting";
} else {
theAnswer = verb_infinitive.substring(0, verb_infinitive.length()) + "ing";
} }

if(endOfVerb.equals("b")){
if(endOfVerb_1.equals("a") || endOfVerb_1.equals("e")|| endOfVerb_1.equals("i")|| endOfVerb_1.equals("o")|| endOfVerb_1.equals("u")){
theAnswer = verb_infinitive + "bing";
} else {
theAnswer = verb_infinitive.substring(0, verb_infinitive.length()) + "ing";
} }

if(endOfVerb.equals("g")){
if(endOfVerb_1.equals("a") || endOfVerb_1.equals("e")|| endOfVerb_1.equals("i")|| endOfVerb_1.equals("o")|| endOfVerb_1.equals("u")){
theAnswer = verb_infinitive + "ging";
} else {
theAnswer = verb_infinitive.substring(0, verb_infinitive.length()) + "ing";
} }

if(endOfVerb.equals("p")){
if(endOfVerb_1.equals("a") || endOfVerb_1.equals("e")|| endOfVerb_1.equals("i")|| endOfVerb_1.equals("o")|| endOfVerb_1.equals("u")){
theAnswer = verb_infinitive + "ping";
} else {
theAnswer = verb_infinitive.substring(0, verb_infinitive.length()) + "ing";
} }

// catch t ending weirdos
if(verb_infinitive.equals("cheat") || verb_infinitive.equals("float") ||
verb_infinitive.equals("greet") || verb_infinitive.equals("heat") ||
verb_infinitive.equals("sprout")|| verb_infinitive.equals("repeat") ||
verb_infinitive.equals("visit") || verb_infinitive.equals("treat") ||
verb_infinitive.equals("agree") || verb_infinitive.equals("suit") ){
theAnswer = verb_infinitive +"ing"; }

// catch m ending weirdos
if(verb_infinitive.equals("jam") || verb_infinitive.equals("program") ||
verb_infinitive.equals("hum") || verb_infinitive.equals("drum") ||
verb_infinitive.equals("dam") ){
theAnswer = verb_infinitive +"ming";
}

// weirdos ending in e
if(verb_infinitive.equals("agree") || verb_infinitive.equals("disagree") ||
verb_infinitive.equals("guarantee") || verb_infinitive.equals("lie") ||
verb_infinitive.equals("queue")|| verb_infinitive.equals("tie") ){
theAnswer = verb_infinitive +"ing";

}

// weirdos ending in p
if(verb_infinitive.equals("develop") || verb_infinitive.equals("heap") ||
verb_infinitive.equals("peep")){
theAnswer = verb_infinitive +"ing";
}

// weirdos ending in n
if(verb_infinitive.equals("ban") || verb_infinitive.equals("grin") || verb_infinitive.equals("man") || verb_infinitive.equals("plan") || verb_infinitive.equals("sin") ){
theAnswer = verb_infinitive +"ning";
}
// just weird
if(verb_infinitive.equals("stir") || verb_infinitive.equals("star")){
theAnswer = verb_infinitive +"ring";

}

if(verb_infinitive.equals("glue") ){
theAnswer = verb_infinitive +"ing";

}

return theAnswer;
} // end of method gerund
}

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s