Android Java saving to file returning IOException - java

i know there's probably tons of these going around (and believe me i went through a lot when trying to get this right) but i cannot seem to get this going.
I'm trying to save a list of custom objects (cars) into a file on local storage and later on load them back by clicking a button.
I've been trying to use the FileOutputStream based on many sources i've read. of some reason this worked fine when tested using simple list of strings but when it comes to my self made objects i keep getting IO exception when trying to save. if anyone can take a look at my code and tell me what am i missing (spent over 20hr trying to sort this one myself with no luck).
P.S - saving and loading methods are being called by buttons which work fine so code was neglected.
public class Main extends Activity implements OnClickListener{
public static List<Car> carList = new ArrayList<Car>();
String FILENAME = "carListFile";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonView_saveb;
Button buttonView_loadb;
buttonView_saveb = (Button)findViewById(R.id.saveb);
buttonView_loadb = (Button)findViewById(R.id.loadb);
buttonView_saveb.setOnClickListener(this);
buttonView_loadb.setOnClickListener(this);
#Override
public void onClick(View view){
switch (view.getId()){
case R.id.saveb:
SaveData();
break;
case R.id.loadb:
LoadData();
break;
}
}
public void LoadData(){
ArrayList<car> toReturn;
FileInputStream fis;
try {
fis = openFileInput(FILENAME);
ObjectInputStream oi = new ObjectInputStream(fis);
toReturn = (ArrayList<Car>) oi.readObject();
oi.close();
} catch (FileNotFoundException e) {
System.out.println("Failed to load, file '"+FILENAME+"' not found");
} catch (ClassNotFoundException e) {
System.out.println("Failed to load from file '"+FILENAME+"', Class not found");
} catch (IOException e) {
System.out.println("Failed to load from file '"+FILENAME+"', I/O Exception")
}
}
// this method return io exception, no idea why
public void SaveData(){
try {
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
ObjectOutputStream of = new ObjectOutputStream(fos);
of.writeObject(diveLog);
of.flush();
of.close();
} catch (FileNotFoundException e) {
System.out.println("Failed to save, file '"+FILENAME+"' not found");
} catch (IOException e) {
System.out.println("Failed to save to file '"+FILENAME+"', I/O Exception");
}
}
}
no matter what i do this keep getting me IO exceptions.
also note that i have a button which is adding cars to the list so i wont save an empty list (although i don't see why that should matter).

Thanks to everyone who took the time to help me.
As it seems after adding the following line:
e.printStackTrace();
to the exception catchers and following the logCat output i found that Car could not be serialized. A quick research revealed that, since Car object came from a class (Car.java), that class has to implement 'serializable' in order to get serialized and later on be deserialized.
so at the Car.java file :
public class Car implements Serializable {
I came across a good explanation for the whole process, perfect for someone as new to java as i am:
https://www.youtube.com/watch?v=6MisF1sxBTo

Related

Score keeping for a basic Minecraft plugin

I can't seem to find a way to store a simple scoring system for a user identifier and their score. I've tried storing it in a string and in a file, but I couldn't get it to work properly.
Here's the relevant code
public class ShiftGame extends JavaPlugin implements Listener {
public void onEnable() {
getServer().getPluginManager().registerEvents(this, this);
}
#EventHandler
public void onPlayerSneak(PlayerToggleSneakEvent e){
Player player = e.getPlayer();
String name = player.getDisplayName();
if(e.isSneaking() == false){
try {
PrintWriter out;
System.out.println("Writing to data file..");
out = new PrintWriter("PlayerShiftAmounts.txt");
out.println(name);
out.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
}
}
}
Minecraft has some config file class: FileConfiguration and YamlConfiguration, so I think you don't need to use java.io.*, and realy needn't to worry about file writing.
FileConfiguration config = YamlConfiguration.loadConfiguration(file);
If you sure have to log something many times a second , I think you should try to use cache or just make a variable in Java , instead of writing into file or database every time.. I don't know why you need to log a list of which player are pressing shift?

Multiple files transfer protocol with "authentication" from Android to Server

I'm a newbie programmer looking for a way to implement a simple file transfer protocol on Android.
Problem:
Several Android phones need to connect to a server to receive/send a series of XML files saved in internal storage. The server needs to know which phone is requesting a connection so that it can save the files in the correct folder.
Possible solution/algorithm:
There are various tutorials/examples on how to send a file to a server, but none of them seem to implement some kind of "authentication".
Ideally I would like to implement the following (I'll use a metaphor):
Phone: Hello.
Server: Hi. Who are you and what do you want? [send/receive]
Phone A: I'm phone A and I would like to send files.
Server: How many files do you want to send, Phone A?
Phone A: 6 files, [+extra data like total size or whatever]
Server: Alright, you can begin the transfer.
Phone A: Transfers...
Server: I've succesfully received 6 files, have a good day. [stores the files in a PhoneA folder]
Phone A: Bye! [closes connection]
I realise this could very likely be made a lot more efficient, but I don't know where to begin...
Is it even possible to initiate a connection with a server and interact multiple times while waiting for responses?
Question :
Could anyone push me in the right direction somehow? Do I write my own protocol or can this be done with standard functionality? What are the best/easiest existing protocols for this kind of implementation?
I've found this article interesting but I don't see how it could be used for multiple files with authentication
Any help would be much appreciated!
This is easier than you think using old-school FTP, which I've used with success in collecting data from apps, and your server will surely support it.
Get a unique ID for each Android device using enter link description here. You get a 64-bit number (as a hex string) that is randomly generated on each device’s first boot. It's supposedly constant for the life of the device.
Import Apache Commons FTP and use the method describe here to create a directory name inside your working directory on the server with a name matching the unique id.
Use the same library to upload the files using FTP. You'll find many example of how to do this. It takes very minimal code.
Unlike your chat scenario, this is a very client-side solution, and phones you might not want to could upload files -- there's no blacklist -- but it's easy to implement.
For those interested in (terrible) code to perform various FTP functions, here's what worked for me.
It requires the apache commons ftp jar file which can be found on the internet.
//Button that starts it all
public void updateWorkordersList(View view) {
if (!CheckNetworkConnection.isOnline()) {
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(this);
String connectionString = prefs
.getString("connection_string", null);
String userName = prefs.getString("FTPusername", null);
DownloadFilesTask task = new DownloadFilesTask(connectionString,
userName);
task.execute();
Fragment frg = null;
frg = getFragmentManager()
.findFragmentByTag("buttonsContainer");
final FragmentTransaction ft = getFragmentManager()
.beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();
}
}
private class DownloadFilesTask extends AsyncTask<Void, Void, Boolean> {
private FTPClient mFtpClient = new FTPClient();
private FTPFile[] mFileArray;
private String _address;
private String _user;
private String _pass;
public DownloadFilesTask(String ip, String user) {
_address = ip;
_user = user;
}
#Override
protected Boolean doInBackground(Void... params) {
try {
mFtpClient.setConnectTimeout(10 * 1000);
mFtpClient.connect(InetAddress.getByName("insert server here"));
boolean status = mFtpClient.login("username", "password");
if (FTPReply.isPositiveCompletion(mFtpClient.getReplyCode())) {
mFtpClient.setFileType(FTP.ASCII_FILE_TYPE);
mFtpClient.enterLocalPassiveMode();
mFileArray = mFtpClient.listFiles();
}
} catch (SocketException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//Download All Files
if (FTPReply.isPositiveCompletion(mFtpClient.getReplyCode())) {
File directory = null;
directory = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS).getPath());
for (FTPFile file : mFileArray) {
OutputStream outputStream = null;
try {
outputStream = new BufferedOutputStream(
new FileOutputStream(directory + "/"
+ file.getName()));
mFtpClient.setFileType(FTP.BINARY_FILE_TYPE);
mFtpClient.retrieveFile(file.getName(), outputStream);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
//Upload All Files
if (FTPReply.isPositiveCompletion(mFtpClient.getReplyCode())) {
File directory = null;
directory = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS).getPath() + "/srvReady");
for (File file : directory.listFiles()) {
try {
FileInputStream srcFileStream = new FileInputStream(directory + "/" + file.getName());
boolean status = mFtpClient.storeFile(_user + "/" + file.getName(),
srcFileStream);
srcFileStream.close();
if (status){
file.delete();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
try {
mFtpClient.logout();
mFtpClient.disconnect();
} catch (Exception e) {
// TODO: handle exception
}
return true;
}
protected void onProgressUpdate(Integer... progress) {
}
protected void onPostExecute(Boolean result) {
}
}
I figure this could be of some use might someone ever come across a similar problem.

Reusing ObjectOutputStreams in Java

I am reusing ObjectOutputStream to send updates between the two clients, this is the server code,
public void run() {
try {
toPlayer1.writeBoolean(true);
toPlayer1.flush();
while (true) {
try {
found = (boolean[][]) fromPlayer1.readObject();
player1Int = fromPlayer1.readInt();
} catch (Exception ex) {
// Handle exception here...
}
if (isWon(player1Int)) {
toPlayer1.writeInt(P1_WON);
toPlayer1.flush();
toPlayer2.writeInt(P1_WON);
toPlayer2.flush();
sendMove(toPlayer2, found, player1Int);
break;
} else {
toPlayer2.writeInt(CONTINUE);
toPlayer2.flush();
sendMove(toPlayer2, found, player1Int);
}
try {
found = (boolean[][]) fromPlayer2.readObject();
player2Int = fromPlayer2.readInt();
} catch (Exception ex) {
// Handle exception here...
}
if (isWon(player2Int)) {
toPlayer1.writeInt(P2_WIN);
toPlayer1.flush();
toPlayer2.writeInt(P2_WIN);
toPlayer2.flush();
sendMove(toPlayer1, found, player2Int);
break;
} else {
toPlayer1.writeInt(CONTINUE);
toPlayer1.flush();
sendMove(toPlayer1, found, player2Int);
}
}
} catch (IOException ex) {
System.err.println(ex);
}
}
private void sendMove(ObjectOutputStream out, boolean[][] found, int score) throws IOException {
try {
out.reset();
out.writeObject(found);
out.writeInt(score);
out.flush();
} catch (Exception ex) {
// Handle exception here...
}
out.writeInt(score);
}
the problem seems to be that some messages are not being delivered correctly, any suggestions? Am I using the flush() correctly? I have added reset(); it is still not working
update, these are the streams:
public void run() {
try {
toPlayer1 = new ObjectOutputStream(player1.getOutputStream());
fromPlayer1 = new ObjectInputStream(player1.getInputStream());
toPlayer2 = new ObjectOutputStream(player2.getOutputStream());
fromPlayer2 = new ObjectInputStream(player2.getInputStream());
regards, c
If you want an object or objects to be sent again, you need to call reset() on the ObjectOutputStream object.
The problem that reset() solves is that when you send an object in a object stream, the protocol attempts to preserve object identity. The first time you send it, the stream sends the object state. Subsequent times, it just sends a marker that says (in effect) "use this object that I sent you previously".
The reset() method says (in effect) to the ObjectOutputStream ... "forget about all objects that I sent previously".
So if you want to send the same object twice, you need to do something like this:
out.writeObject(found);
// change the state of 'found'
out.reset();
out.writeObject(found);
Note that this doesn't affect primitive values sent using their corresponding write methods. Primitive values don't have "identity" and are sent literally each time.
I should also point out that the following is very bad practice.
} catch (Exception ex) {
}
You are silently ignoring all exceptions. This is lazy and dangerous, and you are likely to come to regret it. (Don't do it even in sample code in SO Questions ... 'cos someone might copy your bad code or some Java beginner might emulate your bad habits.)

java.lang.NullPointerException Error in Robolectric tests

I'm trying to test a method for saving to the internal memory on android but All my tests for it are coming up as "Test in error" when I package the app with Maven, when I check the individual test results it says there was a java.lang.NullPointerException in the class, From what I've read it might be something to do with the context that I pass in but I've tried several different methods on generating a context and nothing's worked so far, any advice on how to get these to work would be greatly appreciated. The code for the test:
#RunWith(RobolectricTestRunner.class)
public class SaveSystemTest {
SaveSystem testSS;
Route testRoute;
#Before
public void setup()
{
testRoute = new Route(new LatLng(54.6279022,-5.9146021), new LatLng(54.6279022,-5.9146021),"testRoute");
testSS = new SaveSystem("testSave",Robolectric.application.getApplicationContext());
}
#org.junit.Test
public void saveTest()
{
boolean b = testSS.save(testRoute);
assertTrue(b);
}
The Code for saveSystem.save (Didn't originally have the try catch but I added that trying to get this to work):
public boolean save(Route file)
{
routeList.add(file);
try
{
if(saveToMemory())
return true;
else
return false;
}catch (Exception e)
{
Log.d("SaveSystem", "Exception: " + e);
return false;
}
}
Code for saveSystem.saveToMemory:
private boolean saveToMemory()
{
try
{
FileOutputStream fos = context.getApplicationContext().openFileOutput(saveFileName,Context.MODE_PRIVATE);
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(routeList);
os.close();
return true;
}catch (Exception e)
{
Log.d("SaveSystem", "Exception: " + e);
return false;
}
}
If you use members in your class which are included via Dependency Injection, you should provide a way to pass in a mocked Object for this context, since they are only injected in runtime of your application and not in your tests.
For a good mocking library, have a look at Mockito.
Stupid mistake, turns out I hadn't initialised the "routeList" List correctly.

Music in Java (start/stop)

Ok, so I am making a game and the music changes when you are in different regions or if there is an interruption, like with an AI.
So I have JUST learned how to make music showup in my program, and now I am trying to make it stop, but I am unsure how to, below is a snippet of code where the music plays and then I try to overwite it with new music when an action occurs.
public static void songs(String word) {
String temp = word;
if (temp.equals("start")) {
try {
try {
blah = new FileInputStream("C:/Users/Austin/Desktop/Storage/programimages/game/battle.wav");
} catch (Throwable e) {
e.printStackTrace();
}
AudioStream as = new AudioStream(blah);
AudioPlayer.player.start(as);
System.out.println("going");
} catch (IOException e) {
System.err.println(e);
}
}
if (temp.equals("stop")) {
try {
try {
blah = new FileInputStream("C:/Users/Austin/Desktop/Storage/programimages/game/silence.wav");
} catch (Throwable e) {
e.printStackTrace();
}
AudioStream as = new AudioStream(blah);
AudioPlayer.player.stop(as);
System.out.println("stopping");
} catch (IOException e) {
System.err.println(e);
}
}
}
This is the only method I have been able to find that has the music play, but if you guys have any other suggestions please let me know.
Again, I want to have sound affects and music going, and right now all that happens is one song will play, and it will not stop under any circumstance until it hits the very end of its length. I want to be able to stop songs whenever a new one should come on, and also allow sound affects to pop up.
Thanks!
(since I am stuck on this and need an answer now I will probably repost on one or two more java sites so I can get a response ASAP, thank you though!!!!)
EDITED CODE: (still does not stop the current stream, any more suggestions appreciated)
public static void songs(String word) throws IOException {
String temp = word;
if (temp.equals("go")) {
try {
blah = new FileInputStream("C:/Users/Austin/Desktop/Storage/programimages/game/battle.wav");
} catch (Throwable e) {
e.printStackTrace();
}
AudioStream as = new AudioStream(blah);
AudioPlayer.player.start(as);
System.out.println("going");
}
if (temp.equals("stop")) {
//don't try and do things with a null object!
if (as != null) {
AudioPlayer.player.stop(as);
System.out.println("stopping1");
}
System.out.println("stopping2");
AudioPlayer.player.stop(as);
}
}
Currently you're creating a new AudioStream in your stop branch and calling the stop method using this. This is a different object to the one that is currently playing. Try making the AudioStream a class variable, and calling stop on that instead.
EDIT: at the top of the class containing your code...
class YourClass {
//the class member variable
private AudioStream as;
//[etc...]
In your start branch:
// 'as' has already been defined above
as = new AudioStream(blah);
AudioPlayer.player.start(as);
System.out.println("going");
In your stop branch:
try
{
//don't try and do things with a null object!
if (as != null)
{
AudioPlayer.player.stop(as);
}
System.out.println("stopping");
}
catch (IOException e)
{
System.err.println(e);
}
You may have trouble with the static identifier on your method - if you're calling this from within an instantiated class you don't need this.
I can't even access these sun.audio Objects on my Eclipse IDE--I know they are in rt.jar, but there is header info about them being proprietary and such.
Can the Java Sound library (javax.sound.sampled) handle what you want to do? Both Clip and SourceDataLine allow one to stop playback. That is a more usual way of playing sound, if you want to use native Java.
Playback into is here:
http://docs.oracle.com/javase/tutorial/sound/playing.html
But the documentation, overall, is not exactly rich with examples. There's example code at this site
http://www.jsresources.org/
and plenty of people here who could help if you run into problems with the native Java approach.

Categories

Resources