• Post Reply Bookmark Topic Watch Topic
  • New Topic

Writing a Char Array to a tab delimited file

 
Jax Laakso
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having problems getting my program to write to a file. The character arrays are grabbing the correct information, I can get it to print out to the screen, just not to the file. I've never used FileWriters before, only printed to the screen so I'm beyond stumped. None of the examples I've seen and tried have used. It will make the file, just won't write to it. Any help would be grately appreciated. I'll post the code so maybe someone could point me in the right direction.
Jax

import java.io.*;

public class FindCosts
{
// Main entry point
static public void main(String[] args)
{
try
{

String fileName = "wipcost.txt";
File input = new File(fileName);
FileReader fr = new FileReader(input);
BufferedReader br = new BufferedReader(fr);
String line = br.readLine();
String nextline = "";


String outname = "wipfile.txt";
File output = new File("wipfile.txt");
FileWriter fout = new FileWriter(output);
//BufferedWriter bw = new BufferedWriter(fout);


int length = 0;
String word2 = " Ordered by: ";//see SearchWord
String trap = "--------------------------";

char[] Total = new char[13]; //Stores the total from the Wip
char[] Job = new char[12]; //Stores the Job Code
char[] CustCode = new char[5]; //Stores the Customer Code

try
{
while(line != null)
{
if(length == 6)
{
for(int i = 0; i != length; i++)
{
Job[i] = line.charAt(i);
fout.write(Job[i]);

}


}
else if(line.indexOf(word2) != -1)
{

int Count = line.indexOf(word2); //Saves the index character for me
int Searchlen = word2.length();
int start = Count + Searchlen;
int stop = start + 5;
int i = 0; //counter for the array

for(start = Count + Searchlen; start < stop; start++)
{
CustCode[i] = line.charAt(start);
i++;

}

}
else
{
if(line.indexOf(trap) == 0 && nextline.indexOf("Total WIP") == -1)
{
int totIndex = 100; //the index point of the end of the WIP number
int i = 0; //counter for the array and for loop

for(int index = 88; index != totIndex; index++)
{
Total[i] = nextline.charAt(index);
fout.write(Total[i]);

}
}
}

nextline = line;
line = br.readLine();
length = line.length();

}
br.close();
fout.close();

}


catch(NullPointerException e)
{
System.out.println("We've hit the end.");

}
}
catch( IOException ioe )
{
System.out.println( "Problems with reading file: " + ioe.getMessage() + "\n");
}


}

}
 
moazzam rasheed
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this i think it will work for u

public void Write_File(){
try{
// Creates a new output file with the path and file name specified
File output = new File(strOutDirPath , strOutFileName);
// creates a writer object with file writer to write on the file
BufferedWriter out = new BufferedWriter(new FileWriter(output.getPath() , true));
if(!output.isFile()){
System.out.println("Creating "+output.getPath() +" failed");
return;
}
out.write("ur string that u want to write in a file");
out.close();
} // end of try block
// This portion catches any exception if file is not written because of any reason.
catch(IOException e){
System.out.println("The IO exception occured " + e);
} // end of catch block
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!