writing every new char element to a single file if it's overwritten

Andrew f0s3 Kosenko

I need to write some char element to a .txt before it gets overwritten by me in runtime.

I've tried doing like this:

public void keyTyped(KeyEvent keyEvent) {
    char c = keyEvent.getKeyChar();//this gets overwritten in runtime
    try {
        PrintWriter out = new PrintWriter("test.txt");
        out.print(c);
    } catch (FileNotFoundException x) {
        x.printStackTrace();
    }
}

But as you may guess, if i type "java", only 'a'(last char) will be inside of test.txt. Also, when typed "rrr", same letters need to be saved too. Sorry for bad English. Any help is appreciated.

Frakcool

From OP's edits in the question:

SOLUTION!

String s = "";//initializing a variable
s = s.concat(String.valueOf(c));//two-liner elegant solution!

i had stuck on this for two days, so if you were thinking about this more than me and/or have found more ellegant or effactive way, let me know.

Full code example:

String s = "";
public void keyTyped(KeyEvent keyEvent) {
    char c = keyEvent.getKeyChar();
    String s = s.concat(String.valueOf(c));
    try {
        PrintWriter out = new PrintWriter("test.txt");
        out.print(s);
        out.close();
    } catch (FileNotFoundException x) {
        x.printStackTrace();
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python Element Tree Writing to New File

From Dev

Reading and writing to an XML file with a namespace without iterating through every element

From Dev

char function in fprintf matlab: keeping new lines when writing to file

From Dev

ofstream File is being overwritten every iteration

From Dev

Writing an Element to a file

From Dev

Writing an Element to a file

From Dev

Writing integers into a new file

From Dev

How to add program to every single file's "open with..." list?

From Dev

Writing NULL char to file in C

From Dev

Writing to log file: using (... new StreamReader) every time I write a line?

From Dev

Writing to log file: using (... new StreamReader) every time I write a line?

From Dev

Python: pair a single element with every element in a list

From Dev

C# List Elements being overwritten when adding a new element

From Dev

Writing new file in python 2.7

From Dev

Exception writing data into a new file

From Dev

Writing new file in python 2.7

From Dev

Writing a text file in new format

From Dev

Python writing to a new file each new loop

From Dev

Writing char* to binary file using ostream::write

From Dev

Access Violation when writing an char array in a file

From Dev

Writing char array to NetCDF file with julia

From Dev

New file for every Swift class

From Dev

Writing a single file to multiple s3 buckets with gulp-awspublish

From Dev

Is every single file in the /dev directory is a device file?

From Dev

PHPExcel setting date format without writing every single value

From Dev

Python - Writing to a new file from another file

From Dev

Questions about unsigned char, char, BYTE, and file writing

From Dev

Multi threaded single file writing in Java

From Dev

Writing a large matrix in a single file using MPI

Related Related

  1. 1

    Python Element Tree Writing to New File

  2. 2

    Reading and writing to an XML file with a namespace without iterating through every element

  3. 3

    char function in fprintf matlab: keeping new lines when writing to file

  4. 4

    ofstream File is being overwritten every iteration

  5. 5

    Writing an Element to a file

  6. 6

    Writing an Element to a file

  7. 7

    Writing integers into a new file

  8. 8

    How to add program to every single file's "open with..." list?

  9. 9

    Writing NULL char to file in C

  10. 10

    Writing to log file: using (... new StreamReader) every time I write a line?

  11. 11

    Writing to log file: using (... new StreamReader) every time I write a line?

  12. 12

    Python: pair a single element with every element in a list

  13. 13

    C# List Elements being overwritten when adding a new element

  14. 14

    Writing new file in python 2.7

  15. 15

    Exception writing data into a new file

  16. 16

    Writing new file in python 2.7

  17. 17

    Writing a text file in new format

  18. 18

    Python writing to a new file each new loop

  19. 19

    Writing char* to binary file using ostream::write

  20. 20

    Access Violation when writing an char array in a file

  21. 21

    Writing char array to NetCDF file with julia

  22. 22

    New file for every Swift class

  23. 23

    Writing a single file to multiple s3 buckets with gulp-awspublish

  24. 24

    Is every single file in the /dev directory is a device file?

  25. 25

    PHPExcel setting date format without writing every single value

  26. 26

    Python - Writing to a new file from another file

  27. 27

    Questions about unsigned char, char, BYTE, and file writing

  28. 28

    Multi threaded single file writing in Java

  29. 29

    Writing a large matrix in a single file using MPI

HotTag

Archive