- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在尝试将字符串“testing”添加到 inventory.txt,只是为了查看我的文件是否已成功 append 。由于某种原因,它不会 append !它编译并成功运行,但“测试”从未出现在我的文件中。我在网上搜索了一堆,我认为我做得对......有人可以尝试帮助我找到我的错误吗?谢谢!
/**
Add in javadoc comments
*/
//import statements
import java.io.*;
import java.util.*;
public class Try {
public static void main(String[] args){
//variables
Scanner kb = new Scanner(System.in);
boolean valid = false;
int mainSelect = 0;
int itemOption = 0;
boolean valid2 = false;
boolean returnToMain = false;
String theName = "";
double thePrice = 0;
int theQuantity = 0;
double currBal;
String lampName;
double lampPrice;
int lampQuantity;
String chairName;
double chairPrice;
int chairQuantity;
String deskName;
double deskPrice;
int deskQuantity;
int sellAmnt;
boolean valid3 = false;
//create file
try{
PrintWriter outputFile = new PrintWriter("inventory.txt");
outputFile.println("3000.0");
outputFile.println("Lamps 15.3 400");
outputFile.println("Chairs 19.95 250");
outputFile.println("Desks 95.0 300");
outputFile.close();
}
catch(IOException e){
System.out.println("File cannot be created.");
}
//read data in from file
try{
File file = new File("inventory.txt");
Scanner inFile = new Scanner(file);
currBal = inFile.nextDouble();
lampName = inFile.next();
lampPrice = inFile.nextDouble();
lampQuantity = inFile.nextInt();
chairName = inFile.next();
chairPrice = inFile.nextDouble();
chairQuantity = inFile.nextInt();
deskName = inFile.next();
deskPrice = inFile.nextDouble();
deskQuantity = inFile.nextInt();
inFile.close();
//present user with main menu
do{
System.out.println("Current Balance: $" + currBal);
System.out.println("\t1. " + lampName + "\t\t(" + lampQuantity + " at $" + lampPrice + ")");
System.out.println("\t2. " + chairName + "\t\t(" + chairQuantity + " at $" + chairPrice + ")");
System.out.println("\t3. " + deskName + "\t\t(" + deskQuantity + " at $" + deskPrice + ")");
System.out.println("\t0. Exit");
while(valid == false){
System.out.print("\nPlease enter choice: ");
try{
mainSelect = kb.nextInt();
if(0 <= mainSelect || mainSelect >= 3){
valid = true;
}
else{
System.out.println("That is not a valid selection. Try again.");
}
}
catch(InputMismatchException ime){
System.out.println("That is not a valid selection. Try again.");
kb.next();
}
}
//present user with second menu
switch(mainSelect){
case 1:
theQuantity = lampQuantity;
thePrice = lampPrice;
theName = lampName;
break;
case 2:
theQuantity = chairQuantity;
thePrice = chairPrice;
theName = chairName;
break;
case 3:
theQuantity = deskQuantity;
thePrice = deskPrice;
theName = deskName;
break;
case 0:
System.exit(0);
break;
}
System.out.println("\nCurrent balance: $" + currBal);
System.out.println("Current Quantity: " + theQuantity);
System.out.println("Current price: $" + thePrice);
System.out.println("1. Sell " + theName);
System.out.println("2. Buy " + theName);
System.out.println("3. Change price");
System.out.println("0. Return to main menu");
while(valid2 == false){
System.out.print("\nPlease enter choice: ");
try{
itemOption = kb.nextInt();
if(0 <= itemOption || itemOption >= 3){
valid2 = true;
}
else{
System.out.println("That is not a valid selection. Try again.");
}
}
catch(InputMismatchException ime){
System.out.println("That is not a valid selection. Try again.");
kb.next();
}
}
//Action: sell
if(itemOption == 1){
do{
System.out.print("Amount to sell (current quantity: " + theQuantity + "): ");
sellAmnt = kb.nextInt();
returnToMain = true;
try{
sellAmnt = kb.nextInt();
if(0 <= sellAmnt || sellAmnt >= theQuantity){
valid3 = true;
try{
//append file
FileWriter fw = new FileWriter("inventory.txt", true);
PrintWriter pw = new PrintWriter(fw);
pw.write("testing"); //not working!!!!!!!!!!
pw.close();
}
catch(IOException e){
System.out.println("File not found.");
}
}
else{
System.out.println("\nThat amount is not within quantity bounds. Please enter a number within bounds.");
}
}
catch(InputMismatchException ime){
System.out.println("That is not a valid number. Try again.");
kb.next();
}
}while(valid3 == false);
}
//Action: buy
if(itemOption == 2){
returnToMain = true;
}
//Action: change price
if(itemOption == 3){
returnToMain = true;
}
}while(returnToMain == true);
}
catch(FileNotFoundException e){
System.out.println("Cannot find file.");
}
}
}
最佳答案
这里:
//create file
try{
PrintWriter outputFile = new PrintWriter("inventory.txt");
outputFile.println("3000.0");
outputFile.println("Lamps 15.3 400");
outputFile.println("Chairs 19.95 250");
outputFile.println("Desks 95.0 300");
outputFile.close();
}
catch(IOException e){
System.out.println("File cannot be created.");
}
您总是重新创建文件并向其中添加内容,然后保存它。无论您如何处理该文件,每次执行应用程序时都会重写该文件。
作为建议,只有当您的文件不存在时才应执行这段代码。
关于java - 无法使用java追加到文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28441223/
在 Python 中,我可以附加到一个空数组,例如: >>> a = [] >>> a.append([1,2,3]) >>> a.append([1,2,3]) >>> a [[1, 2, 3],
我正在阅读(并慢慢尝试)在 php 中与 txt 文件交互的方法。我已经尝试过追加,它将数据添加到txt文件的末尾但是 a+ 与 a 有何不同 在 w3schools 中它说: 一个 append 。
我想执行一个非常简单的操作:合并两个形状文件。具体来说,我有美国每个州的人口普查区域形状文件,我想将它们合并到一个形状文件中。最终,我想获取组合的形状文件并在一组经纬度坐标上执行叠加,以确定我的坐标属
当我们使用 append 和 cut 运算符时会出现什么问题? append2([],L,L):-!. append2([H|T],L,[H|TL]):-append2(T,L,TL).
我有一个函数处理程序: function handler(data) { console.log(`1. ${data}`); } 我想在相同的范围内附加或重新定义,如下所示: let old
我目前正在使用很多这样的内容来重构应用程序: StringBuffer buff1 = new StringBuffer(""); buff1.append("some value A"); buff
我正在编写一些代码来对不同类型的啤酒进行一些计算。我有一个使用 GUI 的主类,并有一个 JTextArea 来打印输出。在主类中调用追加工作得很好,但是当我尝试从外部类调用追加来写入文本区域时...
我有一个像这样的 jquery block 。渲染 html 后,我看到 标签立即打开和关闭,同样的方式,立即打开和关闭。我在他的代码中做错了什么吗?有更好的方法来实现这个吗? 谢谢 $.each(f
我在尝试克隆父 div 然后将其直接附加到其自身下方时遇到一个问题。只要最后一个节点是,我的函数就可以正常工作如此选择: A B C 将导致 A A.1
我正在尝试在现有 td 末尾附加一个 td。下面是以下代码(我在 jqgrid 中执行)。 $("#list_toppager_center tr:first td:eq(7)").append("C
我正在尝试在 jQuery 中的以下追加方法上设置超时。我尝试过的所有操作都不断返回Uncaught SyntaxError:意外的标识符 这是我的代码: setTimeout("$('#us
我想用 c 打开一个文件,然后向其中添加一些内容并关闭它。我只是想知道 fopen 中的 a+ 自动导航到文件的最后一个字符。 最佳答案 是的。 为什么不尝试一下,或者阅读一下手册呢? 这里是:
在我的代码中,我有一个输入字段,它是一个循环的值。 用户在第一个字段中输入所需的值。 用户单击按钮/徽章(单击我添加项目符号)以附加到模式。 根据字段中的输入值显示带有项目符号数的模态框。 例如,如果
是否可以使用 QUrlQuery 在不对 url 进行 strip 化的情况下 append 数据? 使用下面的代码将删除“?”之后的所有内容和结果是: https://foobar.com/Info
好吧,我正在为 iPhone 制作一个简单的聊天应用程序,我很幸运,它运行良好并且看起来很棒但是我有一些问题,一个这样的问题是我向用户显示富文本的方式.. 目前我有一个荒谬的系统,它是这样工作的 {发
在 C# 中格式化我做的字符串: string a = String.Format("/blah/blah/{0}_{1}/blah.html", int1, int2) 在Python中,它会自动将
我有一个 300 万行的 .txt 文件。该文件包含如下所示的数据: # RSYNC: 0 1 1 0 512 0 #$SOA 5m localhost. hostmaster.localhost.
我有一个问题。可以删除使用 javascript 附加添加的元素? 当我尝试删除添加的跨度时,什么也没有发生。 像这样: $(document).ready(function(){ $('#
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
到目前为止这是我的代码,我想做的是说用户输入 1 2 3 然后按 -1,他或她将被要求输入另一组数字,比如 9 8 7,我的程序是什么假设要做的是将它们显示为 1 2 3 9 8 7,而是像这样显示它
我是一名优秀的程序员,十分优秀!