Reading And Writing Files Into Java?
Apr 17, 2015
have to create a file named Lab13.txt. In the file I have 10 random numbers. I have to import the 10 numbers and have to Multiply all the numbers from Lab13.txt by 10 and save all the new numbers a new file named Lab13_scale.txt. so if the number 10 is in lab13.txt it prints 100 to Lab13_scale.txt. how do i get it to Multiply Here is what I have:
Java Code: import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
[Code].....
View Replies
ADVERTISEMENT
May 17, 2014
I have copied a wav file as explained in [URL] ..... When trying to play my new file, it runs but no sound is hear. My reading and writing class is as explained in another post in the site:
This is the function that reads the file:
// read a wav file into this class
public boolean read(DataInputStream inFile) {
myData = null;
byte[] tmpInt = new byte[4];
byte[] tmpShort = new byte[2];
[Code] ....
I can't hear the copied wav file, but still the wav file is correctly copied...
View Replies
View Related
Nov 11, 2014
public class Tester
{
public static void main(String[] args)
{
Product p1 = new Product();
p1.loadFromFile("Monitor.pr");
System.out.println("Actual: " + p1.toString());
System.out.println("Expected: Product [id=12345, name=Monitor, description=A freakin great monitor!]");
p1.setId(11111);
p1.setName("another product");
p1.setDescription("Description of another product!");
[code]....
The expected output is what my program should be doing and the actual output is what it's doing instead. As you can see, my code works for the middle test. But I cannot understand why it won't load the first fileFor the second half, the id and the quantity are separated by a comma... I'm not really sure how to deal with that. As you can see, I tried using a delimiter, but it doesn't seem to be doing any good.
View Replies
View Related
Nov 20, 2014
I have a project where I am required to read and write a vector of bank account objects and I am struggling with this concept. I was able to figure it out when the accounts were not stored in a vector but now that I am dealing with vectorsThis is my best attempt. Even though I know it's wrong, what I am trying to do.write/read methods in main:
public static void readTrans()
{
textArea.setText("");
chooseFile(1);
try
{
FileInputStream fis = new FileInputStream(filename);
ObjectInputStream in = new ObjectInputStream(fis);
for (int index=0; index != fileIndex; index++)
[code]....
View Replies
View Related
Jul 20, 2013
So I am trying to write the output of two different java class files to one txt file while the program runs. The file name is determined before the program is ran, through the command prompt as arguments. How can I get the second class file to edit the same txt file without running into compile errors.
For right now I'm just going to send everything that the second file outputs to a message String variable, so that the Main class outputs to the the text file. I still want to learn how to write to the same text file directly from the second class file.
import java.io.*;
public class Test{
public static void main(String[] args) throws IOException{
int x;
//create a new file internally called doc. But externally labelled the user input
File doc = new File(args[0]);
if (doc.exists()){
[Code] .....
View Replies
View Related
Sep 9, 2014
I've looked at multiple sources and everyone is saying different stuff. Which one should I be using? FileWriter/FileReader, other people was saying PrintWriter, and one even said : "Formatter" which is the one I'm doubting mostly. My purposes for writing files is for like saving maps, saving high scores, etc.
View Replies
View Related
Jan 10, 2014
I have accountsData.txt file in my default package, this is the file it should be writing to and reading from.
There is an ATM class, which has the main in it. From here the program is supposed to work like an atm, you type in your account number and can withdraw or deposit. It is supposed to then write the changes to the file.
//Read from a file.
public void loadAccounts(String inputFileName) throws IOException
{
Scanner fin = new Scanner(new File(inputFileName));
// Variables to store the values.[/color]
int account = 0;
double balance = 0.0;
[Code] .....
View Replies
View Related
Dec 4, 2014
I need to read lastname, firstname, and ssn from a text file and sort them based on oneof those three creiteria. Here is my person class:
public class Person implements Comparable {
private String lastName, firstName, ssn;
private static int sortBy = 1;
public static final int LASTNAME=1, FIRSTNAME=2, SSN=3;
public void setSortMethod(int method) {
if(method != LASTNAME && method != FIRSTNAME && method != SSN) {
throw new IllegalArgumentException();
} else {
[code]....
how to sort my info and store it into an array and then write that info into my new file.
View Replies
View Related
Oct 11, 2014
I'm having a bit of trouble with using the Scanner and the Printwriter. I start with a file like this (1 = amount of Houses in the file)
1
FOR SALE:
Emmalaan 23
3051JC Rotterdam
7 rooms
buyprice 300000
energylevel C
The user gets (let's say for simplicity) 3 options:
1. Add a House to the file,
2. Get all Houses which fullfil requirements (price, FOR SALE / SOLD etc.) and
3. Close the application.
This is how I start:
Scanner sc = new Scanner (System.in);
while (!endLoop) {
System.out.println("Make a choice);
System.out.println("1) Add House");
System.out.println("2) Show Houses");
System.out.println("3) Exit");
int choice = sc.nextInt();
Then I have a switch for all of the three cases. I keep the scanner open, so Java can get the user input (house = for sale or sold, price = ... etc). If the user chose option 1, and all information needed is inputted and scanned, the House will be written to the file (which looks like what I typed above).
For this, I use try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("Makelaar.txt", false)))). This works perfectly (at least so it seems.)
If the user chose option 1, and all requirements are inputted and scanned, the Houses will be read (scanner) from the file and outputted. For this I use the same Scanner sc. This also works perfectly (so it seems atleast).
My problem is as follows: If a House has been added, I can only read the House(s) which were already in the file. Let's say I have added 2 houses, and there were from the start 3 houses. If option 2 is chosen, the first 3 houses will be scanned perfectly. An exception will be caught for the remaining 2 (just added) Houses. How can I solve this? I tried to close the Scanner, and reopening it, but apparently Java doesn't agree with this
View Replies
View Related
Dec 2, 2014
So basically i have to read from a text file and get some information back from that and print to screen. Im quite confused and im not sure what loop i should use first to scan the text and receive certain information back?
View Replies
View Related
Feb 6, 2015
I know that I need to use substrings, but I'm not sure how to implement them. All of the names are red in from the file "employees.txt".
Using the file Employees.txt
1) Scan the file and create user ids for each person according to the following scheme
a) Take the first and last character of the first name, convert them to upper case
b) Take the first and last character of the last name, convert them to upper case
c) Concatenate the results from a and b
i) Example: John Brown would yield => JNBN
d) Add the length of the first name to the length of the last name, disregarding spaces
e) Concatenate the result from step c to the result of step d
i) Example: John Brown would yield => JNBN9
f) Concatenate a four digit number to the end of this.
i) The numbers should begin with 0000 and increment by 1
ii) Example: If John Brown is the fifth name in the list the resulting UserID would be => JNBN90004
2) Display the user ids in four columns with a width of 14
View Replies
View Related
May 26, 2014
So I have a text file, and I want my Java program to store names from the text file. How do I do that? This is what I have so far.
Java Code: import java.util.Scanner;
import java.io.File;
BingoCard[] cards = new BingoCard[n]; //Array of BingoCard objects, n being the length of the Array
Scanner sc = new Scanner(File("Names")); //Names is the name of the file
for(int c = 0; c < cards.length; c++)
cards[c] = new BingoCard(sc.nextLine); mh_sh_highlight_all('java');
Here's the constructor.
Java Code: private string myName;
public BingoCard(String name)
{
myName = name;
} mh_sh_highlight_all('java');
Whenever I compile, I get this error message.
View Replies
View Related
Apr 13, 2014
So here is my code:
while (key != 3) {
if (key == 2) {
System.out.println("Input from a file");
// System.out.print("Enter file name: ");
// String str = expression.nextLine();
int i = 0;
File file = new File(
[code]....
What I need to happen is the file gets read in, the file is in the form:
a * ( b + c ) / 2 + ( 8 * b )
a = 5
b = 10
c = 20
( x + y ) * z
x = 13
y = 21
z = 3
And so on. My code is supposed to start at the beginning and have output like:
Infix: a * b ( b + c ) / 2 + ( 8 * b )
variable values:
a = 5
b = 10
c = 20
**It then needs to store the variable names along with their values in a hash table**
postfix: a b c + * 2 / 8 b * +
value: 155
And it continues reading the file in the same fashion.
My issue is I am not properly extracting the variables and their values from the file. I have the variables in an array list actually, so I have those, but don't know how to efficiently navigate to the values, then store the character and value in a hash table, while protecting against possible blank lines within the file. Thus just calling nextLine()s and next()s wouldn't work to get to values because they would lead to an error.
View Replies
View Related
Nov 1, 2014
I just can't seem to figure it out how to solve this inherently recursive assignment.The task is to check a directory path and read the files within that path; also, if there are more directories within it, we have to go deeper into those directories to read the files within them - if any. What you're looking at is my skeleton of the assignment:
public class SearchingForStrings {
public static void main(String[] args) {
String path = "."; // Default
File sf = new File(path);
String mainDirectory = args[0]; // These two are just
String keyString = args[1]; // command-line arguments
countLinesWithString(sf, mysteriesDirectory, keyString);
[code]....
View Replies
View Related
Jul 24, 2014
I want to read about a 100 files. There are 4 Categories of the files. I also want to create a Database of these files. The 4 Categories are my Columns. My question is how to it. I know what i want to do but not how to write in code.
View Replies
View Related
Apr 10, 2015
how to use a Scanner to read data from a file. Is it possible to also average a set of numbers at the same time its being read?
View Replies
View Related
May 4, 2015
In the program below I'm trying to read the contents of several .txt files in the same diretory and create a new .txt file containing all of the data from each file. My output is generated in the console however my .text file is blank.
public static void main(String[] args) throws IOException {
String target_dir = "C:Files";
String output = "C:Filesoutput.txt";
File dir = new File(target_dir);
File[] files = dir.listFiles();
[Code] .....
View Replies
View Related
Oct 14, 2014
I am currently building a Plugin system for an application and I wanted to add a little security feature.
I have read many tutorials on this and they basically all came down to the same answer:
"Its complicated: You need a custom class loader and security manager and ... and even then its still tricky"
So I thought, before I load the classes dynamically I will simply read the class files. Why bother with a SecurityManager or ClassLoader if I can simply whitelist all allowed classes and search for all illegal Class access before I even load anything.
View Replies
View Related
May 4, 2015
How can I Cache the data I'm reading from a collection of text files in a directory using a TreeMap? Currently my program reads the data from several text files in a directory and the saves that information in a text file called output.txt. I would like to cache this data in order to use it later. How can I do this using the TreeMap Class? These are the keys,values: TreeMap The data I'd like to Cache is (date from the file, time of the file, current time).
import java.io.*;
public class CacheData {
public static void main(String[] args) throws IOException {
String target_dir = "C:Files";
String output = "C:Filesoutput.txt";
File dir = new File(target_dir);
File[] files = dir.listFiles();
[Code] ....
View Replies
View Related
Apr 29, 2015
In the current program I am trying to read multiple text files from a folder. I keep getting the following syntax error
"Syntax error on token ";", { expected after this token".
I highlighted the line where im getting this error in red.
import java.io.*;
import java.util.*;
public class CacheData {
// Directory path here
String path = "C:myfiles*.txt";
[Code] ....
View Replies
View Related
Oct 26, 2014
What step to know to develop software..
View Replies
View Related
Nov 26, 2014
for x = 0 to 9
set stars = "*"
set count = 0
while count < x
stars = stars + "*"
count = count + 1
endwhile
display stars
endfor
I need to change this pseudocode to Java, but I'm new, been trying for ages but can't get it right.
View Replies
View Related
Oct 19, 2014
how to print from bluej onto actual paper? how to do the formatting and configuration etc.
View Replies
View Related
Jul 27, 2014
I have a java code that should sort an array of names based on the last name. e.g jane a, jane b, jane z, jane d should be jane a, jane b, jane d, jane z. I have the following code but for some reasons, the s1 in the comparator method is always null.
public class ShuffleName {
public static void sortNames(String[] names){
Arrays.sort( names, new Comparator<String>() {
public int compare( String s1, String s2 ) {
String s1last = s1.split("s+")[1];
String s2last = s2.split("s+")[1];
return s1last.compareTo(s2last);
[code]....
View Replies
View Related
Aug 5, 2014
I am currently working on a java project, this involves me writing some code for a project, below are my attempts at coding so far:
/**
* Prints out details of all animal in the zoo.
*
*/
public void printAllAnimals() {
System.out.println("
Details for all animals in Zoo " + zooId);
System.out.println( "==================================");
[code]....
I currently cannot get the printallanimals() method to work as it should when executing the method printallanimals it just opens a filedialog box, when it is suppose to use the Collection object c,so that animals stored in the zoo can easily be checked.
View Replies
View Related
Dec 20, 2014
I'm doing this assignment in which i have to write some products in csv file...but as u can see in csv file two products are same "Cooking Oil"..so any method that can add two same product's quantity and their amount and write them in file
import java.util.*;
import java.io.*;
public class SalesbyTransactionMonth{
private static final String fileName = "Data.txt";
private static final String fileName1 = "sales_by_trans_month.csv";
String line = "";
[Code] ....
Attached image(s)
View Replies
View Related