Em Java, tenho texto de um campo de texto em uma variável String chamada "text".
Como posso salvar o conteúdo do "text" variável em um arquivo?
Dê uma olhada na Java File API
um exemplo rápido:
try (PrintStream out = new PrintStream(new FileOutputStream("filename.txt"))) {
out.print(text);
}
Acabei de fazer algo semelhante no meu projecto. Use FileWriter irá simplificar parte do seu trabalho. E aqui você pode encontrar tutorial.
BufferedWriter writer = null;
try
{
writer = new BufferedWriter( new FileWriter( yourfilename));
writer.write( yourstring);
}
catch ( IOException e)
{
}
finally
{
try
{
if ( writer != null)
writer.close( );
}
catch ( IOException e)
{
}
}
Utilize FileUtils.writeStringToFile()
de Apache Commons IO. Não há necessidade de reinventar esta roda em particular.