在JAVA上打开文件 import java.util.Formatter; 导入java.util.Scanner; 公共类JavaApplication3{ 公共静态void main(字符串[]args)引发异常 { 格式化程序输出=新格式化程序(“clients.txt”);//打开文件 扫描仪输入=新扫描仪(System.in);//读取用户输入 int accountNumber;//存储帐号 String firstName;//存储名字 字符串lastName;//存储姓氏 双倍余额;//存储帐户余额 System.out.printf(“%s\n%s\n%s\n%s\n%s\n\n”, “要终止输入,请键入文件结束指示符”, “当系统提示您输入时。”, “在UNIX/Linux/Mac OS X上键入d,然后按Enter”, “在Windows上键入z,然后按Enter”); System.out.printf(“%s\n%s”, “输入账号(>0)、名字、姓氏和余额。”, "? " ); while(input.hasNext())//循环直到文件结束指示符 { //检索要输出的数据 accountNumber=input.nextInt();//读取帐号 firstName=input.next();//读取名字 lastName=input.next();//读取姓氏 balance=input.nextDouble();//读取余额 如果(帐号>0) { //创造新纪录 output.format(“%d%s%s%.2f\n”,accountNumber, 名、姓、余额); }//如果结束,则结束 其他的 { System.out.println( “帐号必须大于0。”); }//结束其他 System.out.printf(“%s%s\n%s”,“输入帐号(>0)”, “名、姓和余额。”、“?”; }//结束时 output.close();//关闭文件 }//末端总管 }//结束类CreateTextFile
但是,当转到文件位置并打开txt文件时,里面没有任何东西。我使用windows,我的问题是我认为文件的结尾。请帮助我如何用java.tnx在文件上写入。在JAVA上打开文件 import java.util.Formatter; 导入java.util.Scanner; 公共类JavaApplication3{ 公共静态void main(字符串[]args)引发异常 { 格式化程序输出=新格式化程序(“clients.txt”);//打开文件 扫描仪输入=新扫描仪(System.in);//读取用户输入 int accountNumber;//存储帐号 String firstName;//存储名字 字符串lastName;//存储姓氏 双倍余额;//存储帐户余额 System.out.printf(“%s\n%s\n%s\n%s\n%s\n\n”, “要终止输入,请键入文件结束指示符”, “当系统提示您输入时。”, “在UNIX/Linux/Mac OS X上键入d,然后按Enter”, “在Windows上键入z,然后按Enter”); System.out.printf(“%s\n%s”, “输入账号(>0)、名字、姓氏和余额。”, "? " ); while(input.hasNext())//循环直到文件结束指示符 { //检索要输出的数据 accountNumber=input.nextInt();//读取帐号 firstName=input.next();//读取名字 lastName=input.next();//读取姓氏 balance=input.nextDouble();//读取余额 如果(帐号>0) { //创造新纪录 output.format(“%d%s%s%.2f\n”,accountNumber, 名、姓、余额); }//如果结束,则结束 其他的 { System.out.println( “帐号必须大于0。”); }//结束其他 System.out.printf(“%s%s\n%s”,“输入帐号(>0)”, “名、姓和余额。”、“?”; }//结束时 output.close();//关闭文件 }//末端总管 }//结束类CreateTextFile,java,file,Java,File,但是,当转到文件位置并打开txt文件时,里面没有任何东西。我使用windows,我的问题是我认为文件的结尾。请帮助我如何用java.tnx在文件上写入。java.util.Formatter的输出不会立即写入。呼叫和等待数据将立即写入 import java.util.Formatter; import java.util.Scanner; public class JavaApplication3 { public static void main( String[] args )
java.util.Formatter
的输出不会立即写入。呼叫和等待数据将立即写入
import java.util.Formatter;
import java.util.Scanner;
public class JavaApplication3 {
public static void main( String[] args ) throws Exception
{
Formatter output = new Formatter( "clients.txt" ); // open the file
Scanner input = new Scanner( System.in ); // reads user input
int accountNumber; // stores account number
String firstName; // stores first name
String lastName; // stores last name
double balance; // stores account balance
System.out.printf( "%s\n%s\n%s\n%s\n\n",
"To terminate input, type the end-of-file indicator ",
"when you are prompted to enter input.",
"On UNIX/Linux/Mac OS X type <ctrl> d then press Enter",
"On Windows type <ctrl> z then press Enter" );
System.out.printf( "%s\n%s",
"Enter account number (> 0), first name, last name and balance.",
"? " );
while ( input.hasNext() ) // loop until end-of-file indicator
{
// retrieve data to be output
accountNumber = input.nextInt(); // read account number
firstName = input.next(); // read first name
lastName = input.next(); // read last name
balance = input.nextDouble(); // read balance
if ( accountNumber > 0 )
{
// write new record
output.format( "%d %s %s %.2f\n", accountNumber,
firstName, lastName, balance );
} // end if
else
{
System.out.println(
"Account number must be greater than 0." );
} // end else
System.out.printf( "%s %s\n%s", "Enter account number (>0),",
"first name, last name and balance.", "? " );
} // end while
output.close(); // close file
} // end main
} // end class CreateTextFile
它对我来说很有效,至少在Linux上是这样。正在Windows中尝试…我也可以在Windows上使用。请澄清您是在代码运行时查看文件,还是在程序终止后查看文件。如果是前者,那么Satya的回答是合适的——但是如果你只是在程序终止时查看文件,那不能解释它。运行程序时,输出不会反映出来。您应该终止它。代码在最后调用
close()
,这将刷新输出。很好…我也测试过了,很好。当您说“还检查了API”-是什么让您认为关闭格式化程序不会刷新文件输出?注意,文档说如果目标实现了Closeable
,它将调用目标上的close()。但对我来说,在调用flush()
之后,只会反映数据@JonSkeetYou的意思是调用close()时没有任何输出?那肯定会冲掉它。。。我不太明白你的评论。为什么发这个,这是以前答案的重复?
if ( accountNumber > 0 )
{
// write new record
output.format( "%d %s %s %.2f\n", accountNumber,
firstName, lastName, balance );
output.flush();//missing this line
}
java.util.Formatter will not write until flush() method is called.
if ( accountNumber > 0 ){
// write new record
output.format( "%d %s %s %.2f\n",accountNumber,firstName,lastName,balance );
output.flush(); // this is missed.
} // end if`enter code here`