Issue with toString() on float value
in
Contributed Library Questions
•
1 year ago
Hi all,
i try to write an Pvector array to text file. My code works when i use PrintWriter. SInce PrintWriter doesn't have the ability to append data to files (not built in though), i decided to use FileWriter. My code trows an IO exception.
Please have look!
- /**
- This program receives motion data from phone
- */
- import java.io.*;
- import codeanticode.gsvideo.*;
- import controlP5.*;
- import oscP5.*;
- import netP5.*;
- NetAddress myRemoteLocation;
- OscP5 oscP5;
- ControlP5 controlP5;
- ControlWindow controlWindow;
- //PrintWriter output;
- FileWriter output;
- GSMovie front_view;
- GSMovie profile_view;
- //these hold the motion values
- float x, y, z;
- //needed for motion recording
- boolean record;
- int i;
- //data sequences are stored as vectors
- PVector [] mdata = new PVector [2000];
- //to store which movies have been shown
- int [] movies = new int [5];
- /////////////////////////////////////////////////////SETUP/////////////////////////////////////////////
- void setup() {
- //setup the canvas
- size(1280, 480, P3D);
- frameRate(25);
- background(0);
- //make a connection with the OpenSoundControlProtocol
- oscP5 = new OscP5(this, 25000);
- myRemoteLocation = new NetAddress("192.168.1.77", 25000);
- //create an interface framework
- controlP5 = new ControlP5(this);
- controlP5.setAutoDraw(true);
- //create secondary control window for controlling video playback and motion recording
- controlWindow = controlP5.addControlWindow("ControlWindow", 100, 100, 800, 600);
- controlWindow.setTitle("Controls");
- //get a reference to button objects
- Button startButton = controlP5.addButton("Start Record", 1, 50, 50, 250, 100);
- Button stopButton = controlP5.addButton("Stop Record", 2, 50, 175, 250, 100);
- Button storeButton = controlP5.addButton("Store Record", 3, 50, 300, 250, 100);
- Button movieStart = controlP5.addButton("Start Movie", 4, 350, 50, 250, 100);
- Button moviePause = controlP5.addButton("Pauze Movie", 5, 350, 175, 250, 100);
- Button nextMovie = controlP5.addButton("Setup Next Movie", 6, 350, 300, 250, 100);
- //position buttons on the controlwindow
- startButton.setWindow(controlWindow);
- stopButton.setWindow(controlWindow);
- storeButton.setWindow(controlWindow);
- movieStart.setWindow(controlWindow);
- moviePause.setWindow(controlWindow);
- nextMovie.setWindow(controlWindow);
- //populate mdata with actual filenames
- for (int i = 0; i < mdata.length; i++) {
- mdata[i] = new PVector();
- }
- //populate movies with actual filenames
- for (int i = 0; i < movies.length - 1; i++) {
- movies [i] = i + 1;
- }
- //setup first movies
- setNextMovie();
- //get a file writer to store the data in a file corresponding to the performed gesture
- String filename = front_view.getFilename();
- try {
- FileWriter output = new FileWriter("4A.avi", true);
- }
- catch(IOException ioe) {
- println("error: " + ioe);
- }
- //output = createWriter("4A.avi");
- }
- ////////////////////////////////////////////DRAW////////////////////////////////////////////////
- void draw() {
- //draw the movies on the screen
- image(front_view, 0, 0);
- image(profile_view, 640, 0);
- controlP5.draw();
- }
- ///////////////////////////////////////////Button Event Handling/////////////////////////////////
- public void controlEvent(ControlEvent theEvent) {
- int button = int(theEvent.controller().defaultValue());
- switch (button) {
- case 1:
- startRecord();
- break;
- case 2:
- stopRecord();
- break;
- case 3:
- storeRecord();
- break;
- case 4:
- startMovie();
- break;
- case 5:
- stopMovie();
- break;
- case 6:
- setNextMovie();
- break;
- }
- }
- public void start (int theValue) {
- println(theValue);
- }
- ///////////////////////////////////////////Motion Recording//////////////////////////////////////
- public void startRecord() {
- record = true;
- i = 0;
- }
- public void stopRecord() {
- record = false;
- for (int i = 0; i < mdata.length; i++) {
- //why can i not do this?
- //For printwriter i don't need to call toString()
- String out = "[" + mdata[i].x.toString() + "\t" + mdata[i].y + "\t" + mdata[i].z+ "]; ";
- String out_test = "this breaks";
- output.write(out1);
- }
- output.write("\n==========================================================================================================");
- output.flush(); // Write the remaining data
- output.close(); // Finish the file
- }
- public void storeRecord() {
- //TODO
- }
- //////////////////////////////////////////Movie Control/////////////////////////////////////////
- //setup the next movie
- public void setNextMovie() {
- //get a unique integer
- int i = getUniqueInt();
- //create filenames
- String movie_1 = i + "A.avi";
- String movie_2 = i + "B.avi";
- // init movies
- front_view = new GSMovie(this, movie_1);
- profile_view = new GSMovie(this, movie_2);
- }
- //start the movie
- public void startMovie() {
- println("in start movie");
- //TODO get movie to play 2 times
- front_view.loop();
- profile_view.loop();
- }
- //pauze or stop playback
- public void stopMovie() {
- front_view.pause();
- profile_view.pause();
- }
- /////////////////////////////////////////////////////////////////////////Utils////////////////////////////////////////////
- //gets a unique integer
- public int getUniqueInt() {
- int pos = int(random(0, 4));
- //pick movie based on position
- int unique = movies[pos];
- //make the value 0 so that we can identify that the movie has been picked
- movies[pos] = 0;
- //when movie has been picked its value has been set to zero, so we pick again
- if (unique == 0) {
- unique = getUniqueInt();
- }
- return unique;
- }
- //this reads the image from the movie in order to display it
- void movieEvent(GSMovie thisMovie) {
- thisMovie.read();
- }
- //Function reads motion data
- void oscEvent(OscMessage theOscMessage) {
- if (theOscMessage.checkAddrPattern("/motion"))
- {
- if (record == true) {
- mdata[i].x = theOscMessage.get(0).floatValue();
- mdata[i].y = theOscMessage.get(1).floatValue();
- mdata[i].z = theOscMessage.get(2).floatValue();
- i++;
- }
- }
- }
1