user40128: Data can be seen in the console but the data is not being written in the text file. The file is created but there is no data in it. 
Here is my code in Processing,
import processing.serial.*;
Serial myPort;  // Create object from Serial class
String val;     // Data received from the serial port
PrintWriter output;
void setup()
{

   String portName = Serial.list()[0]; 
   myPort = new Serial(this, portName, 9600);
   output = createWriter( "my.txt" );
}
void draw()
{
    if ( myPort.available() > 0) 
     {  // If data is available,
         val = myPort.readStringUntil('\n');       
       output.print( val );
     } 
    println(val); //print it out in the console

 }

and in Arduino,
  int potPin = 0; // initialize analog pin 0 for LM35 temperature sensor
  void setup()
  {
     Serial.begin(9600);// set baud rate at”9600”
  }
  void loop()
  {
     int val;// define variable
     int dat;// define variable
     val=analogRead(0);
     dat=(125*val)>>8;// temperature calculation formula
     Serial.print("Tep:");//
     Serial.print(dat);// output and display value of dat
     Serial.println("C");// display “C” characters
     delay(500);// wait for 0.5 second
   }

Any answers are appreciated. Thank you!

user30039: createWriter description says: 

"For the file to be made correctly, it should be flushed and must be
  closed with its flush() and close() methods"

Source: https://processing.org/reference/createWriter_.html
This question is not about Arduino, but Processing language.

40128: <p>Data can be seen in the console but the data is not being written in the text file. The file is created but there is no data in it. </p>

<p>Here is my code in Processing,</p>

<pre><code>import processing.serial.*;
Serial myPort;  // Create object from Serial class
String val;     // Data received from the serial port
PrintWriter output;
void setup()
{

   String portName = Serial.list()[0]; 
   myPort = new Serial(this, portName, 9600);
   output = createWriter( "my.txt" );
}
void draw()
{
    if ( myPort.available() &gt; 0) 
     {  // If data is available,
         val = myPort.readStringUntil('\n');       
       output.print( val );
     } 
    println(val); //print it out in the console

 }
</code></pre>

<p>and in Arduino,</p>

<pre><code>  int potPin = 0; // initialize analog pin 0 for LM35 temperature sensor
  void setup()
  {
     Serial.begin(9600);// set baud rate at”9600”
  }
  void loop()
  {
     int val;// define variable
     int dat;// define variable
     val=analogRead(0);
     dat=(125*val)&gt;&gt;8;// temperature calculation formula
     Serial.print("Tep:");//
     Serial.print(dat);// output and display value of dat
     Serial.println("C");// display “C” characters
     delay(500);// wait for 0.5 second
   }
</code></pre>

<p>Any answers are appreciated. Thank you!</p>
 30039: <p>createWriter description says: </p>

<blockquote>
  <p>"For the file to be made correctly, it should be flushed and must be
  closed with its flush() and close() methods"</p>
</blockquote>

<p>Source: <a href="https://processing.org/reference/createWriter_.html" rel="nofollow noreferrer">https://processing.org/reference/createWriter_.html</a><br>
This question is not about Arduino, but Processing language.</p>
