Separate two processes on 1 txt: FileReader and FileWriter - filereader

In my code at the first step I write the result of the matcher to the file test1.txt. At the second step I read the content of test1.txt and use a part of speech tagger on it. My problem is that writing and reading to test1.txt works at the same time and that causes double entries in output.txt. How can I do it another way in order that reading from test1.txt begins only when writing to it ends and not at the same time?
while (matcher.find()) {
//create a new file and write to it during the search
try{
pWriter = new PrintWriter(new BufferedWriter(new FileWriter("E:/test/test1.txt", true)));//append any given input
pWriter.println(matcher.group()); //write the result of matcher to the new file
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
if (pWriter != null){
pWriter.flush();
pWriter.close();
}
}
System.out.println(matcher.group()); //Print the result of matcher to console
MaxentTagger tagger = new MaxentTagger("models/english-bidirectional-distsim.tagger");
FileInputStream fstream = new FileInputStream("E:/test/test1.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
//pick up sentences line by line from the file test1.txt and store it in the string sample
while((sample = br.readLine())!=null)
{
//tag the string
String tagged = tagger.tagString(sample);
FileWriter q = new FileWriter("E:/test/output.txt",true);
BufferedWriter out =new BufferedWriter(q);
//write it to the file output.txt
out.write(tagged);
out.newLine();
out.close();
}
}

while (matcher.find()) {
//create a new file and write to it during the search
try{
pWriter = new PrintWriter(new BufferedWriter(new FileWriter("E:/test/test1.txt", true)));//append any given input
pWriter.println(matcher.group()); //write the result of matcher to the new file
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
if (pWriter != null){
pWriter.flush();
pWriter.close();
runReader();
}
}
private void runReader(){
MaxentTagger tagger = new MaxentTagger("models/english-bidirectional-distsim.tagger");
FileInputStream fstream = new FileInputStream("E:/test/test1.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
//pick up sentences line by line from the file test1.txt and store it in the string sample
while((sample = br.readLine())!=null)
{
//tag the string
String tagged = tagger.tagString(sample);
FileWriter q = new FileWriter("E:/test/output.txt",true);
BufferedWriter out =new BufferedWriter(q);
//write it to the file output.txt
out.write(tagged);
out.newLine();
out.close();
}
}
}
This is one of the options, you need to call the function with the reader after the writer is closed.
A much better way is to use a single Thread for reader and writer each
Here is an example how to synchronize two Threads: synchronize two threads in java

Related

Sftp upload from memory stream asp.net

I am trying to upload a csv file that is created from a query and upload via sftp.
I am trying to avoid creating a file and then reading the file to upload it by keeping the data in memory.
Thanks in advance
var customerAddresses = addresses.Select(p => new { p.Customer.Name, p.Customer.AlternateName, p.City, p.StateProvince });
using (var memoryStream = new MemoryStream())
{
//if you pass a file path to streamWriter it creates a csv with the correct format and data
using (var streamWriter = new StreamWriter())
{
using (var csv = new CsvWriter(streamWriter))
{
csv.WriteRecords(customerAddresses);
var fileName = DateTime.UtcNow.ToString(dateFromat) + destinationFileName;
var privateKey = new PrivateKeyFile(sshKeyLocation);
var connectionInfo = new PrivateKeyConnectionInfo(address,
username,
new PrivateKeyFile(sshKeyLocation)
);
memoryStream.Flush();
using (var client = new SftpClient(connectionInfo))
{
client.Connect();
client.ChangeDirectory(serverDirectory);
client.UploadFile(memoryStream, fileName); //is always an empty file
}
}
}
Try adding setting memoryStream Position to beginning with Seek before calling UploadFile and calling streamWriter.Flush();, not memoryStream.Flush():
streamWriter.Flush();
memoryStream.Seek(0, SeekOrigin.Begin);
using (var client = new SftpClient(connectionInfo))
{
client.Connect();
client.ChangeDirectory(serverDirectory);
client.UploadFile(memoryStream, fileName);
}
I never figured out how to get it to work with MemoryStream and changed to to write a file and then read and upload that file.
public HttpResponseMessage Post([FromBody] CustomerIds[] CustomerIds)
{
try
{
if(CustomerIds.Length == 0)
{
throw new Exception("No customer ids passed in post body.");
}
else if (!File.Exists(sshKeyLocation))
{
throw new Exception("Missing ssh file.");
}
if(!Directory.Exists(localDirectory))
{
Directory.CreateDirectory(localDirectory);
}
var customerAddresses = _repository.GetPrimaryAddress(CustomerIds.Select(c => c.Id))
.Select(p => new
{
p.Customer.Name,
p.Customer.AlternateName,
p.City,
p.StateProvince
}
);
if (customerAddresses == null || !customerAddresses.Any())
{
throw new Exception("No customer addresses found for selected customers.");
}
var fileName = DateTime.UtcNow.ToString(dateFromat) + destinationFileName;
var localFilePath = Path.Combine(localDirectory, fileName);
CreateFile(customerAddresses, localFilePath);
UploadFile(localFilePath, fileName);
return new HttpResponseMessage(System.Net.HttpStatusCode.OK);
}
catch(Exception error)
{
logger.Error(error, error.Message);
throw error;
}
}
private void CreateFile(IEnumerable<object> customerAddresses, string filePath)
{
using (TextWriter streamWriter = new StreamWriter(filePath))
using (var csv = new CsvWriter(streamWriter))
{
csv.WriteRecords(customerAddresses);
}
}
private void UploadFile(string localFilePath, string destinationFileName)
{
var connectionInfo = new PrivateKeyConnectionInfo(address,
username,
new PrivateKeyFile(sshKeyLocation)
);
using (var fileStream = new FileStream(localFilePath, FileMode.Open))
using (var client = new SftpClient(connectionInfo))
{
client.Connect();
client.ChangeDirectory(serverDirectory);
client.UploadFile(fileStream, destinationFileName);
}
}
I've run out of time to continue trying to upload using the MemoryStream and will just have to use this solution. Disappointed I couldn't get it to work.

Javafx TextArea setText method using text files

Hi I am trying to create a method to be used in the TextArea setText method for javafx.
I am trying to get a method that does this:
public static void setTextArea(String fileName) {
String line;
try {
FileReader fileReader = new FileReader(fileName);
BufferedReader buffer = new BufferedReader(fileReader);
while ((line = buffer.readLine()) != null) {
out.println(line);
}
buffer.close();
} catch //etc etc
but I can't use it in the setText method because it is a void method.
Can anyone help translate this method so it could work in the TextArea setText method?
-Thanks!
You just pring out the lines to System.out I guess. You have to add up the content of the text file by doing something like this
public static void setTextArea(String fileName) {
String line;
String content;
try {
FileReader fileReader = new FileReader(fileName);
BufferedReader buffer = new BufferedReader(fileReader);
while ((line = buffer.readLine()) != null) {
out.println(line);
content += line;
}
buffer.close();
} catch //etc etc
Then you can either return content or call setText(content) from the TextArea class. If it's a big file, then using StringBuilder would probably be a better idea instead of concatenating each line.
You will have to get the data from the file and and set the data to textArea..
TextArea txtArea = new TextArea();
String data = getDataForTextArea(String fileLocation);
txtArea.setText(data);
public String getDataForTextArea(String fileLocation) {
InputStream inputStream = new FileInputStream(fileLocation);
if (inputStream != null) {
int b;
String txtData = "";
try {
while ((b = inputStream.read()) != -1) {
txtData += (char) b;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
inputStream.close();
}
return txtData;
}
Make sure to check for nullpointerException.

.NET converting file to image to store in a database

Does anyone know of a way in .NET to take an incoming stream of a file and convert it to an image to be stored in the database? (Not sure if this is possible, but wanted to check).
Edit: it is not necessarily an image stream
You need to read the stream into a byte[], then save that into the database.
You can convert the image stream to byte array and store in binary or varbinary data type in database.
Here is a short example of transferring the image into a byte array in C#:
private static byte[] ReadImage(string p_postedImageFileName, string[] p_fileType)
{
bool isValidFileType = false;
try
{
FileInfo file = new FileInfo(p_postedImageFileName);
foreach (string strExtensionType in p_fileType)
{
if (strExtensionType == file.Extension)
{
isValidFileType = true;
break;
}
}
if (isValidFileType)
{
FileStream fs = new FileStream(p_postedImageFileName, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
byte[] image = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
return image;
}
return null;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion

Browser showing wrong data

I have following code which reads data from text file and prints to browser.
But when it gets data like "abc - abc " then in browser it shows junk characters like " ¬タモ"...
What could be the problem ?
fis points to text file. Read data from text file and write to browser.
thnx in advanced.
f = new File(URLDecoder.decode(filePathStr), URLDecoder.decode(fileName));
fis = new FileInputStream(f);
res.setHeader("Pragma", "no-cache");
res.setHeader("Expires", "-1");
res.setHeader("Cache-Control", "no-cache");
req.setCharacterEncoding("UTF-8");
res.setContentType("text/html;charset=UTF-8");
out = res.getWriter();
for (int i = fis.read(); i != -1; i = fis.read()) {
if (i == '\n')
out.print("</BR>");
else
out.write((byte) i);
}
try to output something manually something like
out.print("υτφ-8 chars");
Do they print correctly? If not take a look at this article How to get UTF-8 working in Java webapps? and make sure you follow the 4 steps on how to display UTF-8 Chars in your webpage.
If they print correctly then the problem lies with the FileInputStream and how you read the file.
You can try the following
FileInputStream fis = new FileInputStream(f);
InputStreamReader isr = new InputStreamReader(fis, "UTF8");
NEW UPDATE
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
File f = new File("C:\\TEST.TXT");
FileInputStream fis = new FileInputStream(f);
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
response.setHeader("Pragma", "no-cache");
response.setHeader("Expires", "-1");
response.setHeader("Cache-Control", "no-cache");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
out = response.getWriter();
for (int i = isr.read(), k = 0; i != -1; i = isr.read(), ++k) {
if (i == '\n') {
out.print("</BR>");
} else {
out.write(i);
}
out.flush();
}
} finally {
out.close();
}
}
The above code should work fine, i have tested it
Your file may not be in UTF-8. I would read all file concatenating all bytes in a array, then create an string with it and then I would print it.

how to upload a bigger file using Blobdata

Good day Everyone,
I am having a problem in uploading a file that is greater than 2mb. In here I use a blobdata (bytBLOBData). Here's the code for reference.
try
{
OpenFileDialog myOPD = new OpenFileDialog();
myOPD.Filter = "Image files (.pdf)|.pdf";
if (myOPD.ShowDialog() == DialogResult.OK)
{
pat = myOPD.FileName;
string sPath = myOPD.FileName;
var fInfo = new FileInfo(sPath);
//Open FileStream to read file
FileStream fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read);
//Use BinaryReader to read file stream into byte array.
BinaryReader br = new BinaryReader(fStream);
numBytes = fInfo.Length;
bytBLOBData = br.ReadBytes(Convert.ToInt32(numBytes));
MemoryStream memoryStream = new MemoryStream(bytBLOBData);
FileInfo fi = new FileInfo(myOPD.FileName);
String strName = fi.Name;
txtManual.Text = strName;
txtreferencename.Focus();
br.Close();
fStream.Close();
}
}
catch (Exception)
{
validation.ErrorMessage("load");
}
}
I've research about this problem before, some forums told me that I should use a CommandBehavior.SequentialAccess, but I don't get it right. Any help is highly appreciated :)
Thanks,
what are you trying to do ? I don't understand the purpose of your code, and there is no mention to the file upload. If you simply want to upload a file, use WebClient.UploadFile method.

Resources