how to save the data from iphone application to text file and save in documents
By : vega
Date : March 29 2020, 07:55 AM
help you fix your problem You create a text file let it be "file.txt" and write required data, here "yourString" as follows, code :
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"file.txt"];
NSString *stringWithoutSpaces = [yourString stringByReplacingOccurrencesOfString:@" " withString:@""];
[stringWithoutSpaces writeToFile:filePath atomically:TRUE encoding:NSUTF8StringEncoding error:NULL];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"file.txt"];
NSString *yourString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];
|
Is there an easy way to save arraylist/variables data to a file in Java?
By : Mohammad Akhavan
Date : March 29 2020, 07:55 AM
it should still fix some issue The keyword is serialization. The elements in your List need to implement the Serializable interface, also all the elements in that class. code :
static class Baz implements Serializable {
private static final long serialVersionUID = 1L;
int i;
String s;
public Baz(int i, String s) { this.i = i; this.s = s; }
}
@Test
public void serAL() throws FileNotFoundException, IOException, ClassNotFoundException {
List<Baz> list = Arrays.asList(new Baz(1, "one"), new Baz(2, "two"), new Baz(3, "three"));
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("al.dat"))) {
oos.writeObject(list);
}
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("al.dat"))) {
@SuppressWarnings("unchecked")
List<Baz> serAL = (List<Baz>) ois.readObject();
Assert.assertEquals(3, serAL.size());
Assert.assertEquals(1, serAL.get(0).i);
Assert.assertEquals("one", serAL.get(0).s);
Assert.assertEquals(2, serAL.get(1).i);
Assert.assertEquals("two", serAL.get(1).s);
Assert.assertEquals(3, serAL.get(2).i);
Assert.assertEquals("three", serAL.get(2).s);
}
}
|
how to save csv like text file in an ArrayList to sort by name
By : mahadeb kesh
Date : March 29 2020, 07:55 AM
wish help you to fix your issue Create a class, let's called Person. Then read each line, split it to get the different parts, create a new Person and add it to the list. Then sorting it later would be easy, providing a custom comparator to Collections.sort, telling that you want to sort your List based on person's name.
|
Creating an ArrayList from data in a text file
By : Trần Quang Phúc
Date : March 29 2020, 07:55 AM
wish of those help I am trying to write a program that uses two classes to find the total $ amount from a text file of retail transactions. The first class must read the file, and the second class must perform the calculations. The problem I am having is that in the first class, the ArrayList only seems to get the price of the last item in the file. Here is the input (which is in a text file): , Basically what you have is: code :
while ((line = bufferReader.readLine()) != null) {
substring = line.substring(1, line.lastIndexOf(" ") + 1);
}
priceSubstring = substring.substring(0,substring.indexOf(" "));
quantitySubstring = substring.substring(substring.indexOf(" ") + 1 , substring.lastIndexOf(" ") );
double price = Double.parseDouble(priceSubstring);
double quantity = Double.parseDouble(quantitySubstring);
priceArray.add(price);
quantityArray.add(quantity);
System.out.println(priceArray);
while ((line = bufferReader.readLine()) != null) {
substring = line.substring(1, line.lastIndexOf(" ") + 1);
priceSubstring = substring.substring(0,substring.indexOf(" "));
quantitySubstring = substring.substring(substring.indexOf(" ") + 1 , substring.lastIndexOf(" ") );
double price = Double.parseDouble(priceSubstring);
double quantity = Double.parseDouble(quantitySubstring);
priceArray.add(price);
quantityArray.add(quantity);
System.out.println(priceArray);
}
|
Add text file data to a Arraylist
By : MaxJ
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , I want to add text file data to an ArrayList. This is my text file , Create a new Employee object each time: code :
while ((aLine = in.readLine()) != null) {
String[] tokens = aLine.split(" ");
Employee emp = new Employee();
emp.setUser_id(tokens[0]);
emp.setmDate(tokens[1]);
employeArrayList.add(emp);
out.write(aLine);
out.newLine();
}
|