i'm reading data from serial port for average interval say 1 second,and at the same time writing read data to textArea and textfile,problem is i'm not getting correct data at some time,may be because i'm doing all three process in a single program,how to do writing to text area and text file by separate thred?
this is my code:
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.TooManyListenersException;
import java.util.TreeMap;
import javax.comm.CommPortIdentifier;
import javax.comm.SerialPort;
import javax.comm.SerialPortEvent;
import javax.comm.SerialPortEventListener;
import javax.comm.UnsupportedCommOperationException;
import javax.swing.JOptionPane;
import com.pressure.constants.Constants;
import com.pressure.online.OnlineStartWindow;
public class SerailReader implements SerialPortEventListener {
// DECLARES INPUT STREAM TO READ SRIAL PORT
private InputStream inputStream;
// DECLARES PORT
private CommPortIdentifier port;
private SerialPort serialPort;
// DATE TO CREATE FILE NAME
private static final SimpleDateFormat SDF = new SimpleDateFormat("dd-MM-yy");
private int index = 0;
private File file;
private OnlineStartWindow onlineStartwindow;
private int[] tempIntArray = new int[233];
private int newData = 0;
private String outFolder;
private String filename = "0";
private FileWriter fileWriter;
private BufferedWriter buffOut;
private StringBuffer line;
private String packetFilename;
TreeMap<Integer, Float> channelMap = new TreeMap<Integer, Float>();
ThreadPrintsAndWrites p;
public FileWriter getFileWriter() {
return fileWriter;
}
public void setFileWriter(FileWriter fileWriter) {
this.fileWriter = fileWriter;
}
public BufferedWriter getBuffOut() {
return buffOut;
}
public void setBuffOut(BufferedWriter buffOut) {
this.buffOut = buffOut;
}
// SETTER GETTER TO OnlineStartwindow OBJECT
public OnlineStartWindow getOnlineStartwindow() {
return onlineStartwindow;
}
public void setOnlineStartwindow(OnlineStartWindow onlineStartwindow) {
this.onlineStartwindow = onlineStartwindow;
}
// SETTER GETTER TO SERIALPORT
public SerialPort getSerialPort() {
return serialPort;
}
public void setSerialPort(SerialPort serialPort) {
this.serialPort = serialPort;
}
// ********* connects to serial port ***********//
public void SerialReadmethod(OnlineStartWindow onlineStartwindow,
String outFolderPath) throws Exception {
setOnlineStartwindow(onlineStartwindow);
outFolder = outFolderPath;
// SELECTS PORT NAME SELECTED
port = CommPortIdentifier.getPortIdentifier(getOnlineStartwindow()
.getComPort());
System.out.println("port name " + port);
// CHEAK WETHER SELECTED PORT AVAILABLE OR NOT
if (port.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (port.getName().equals(getOnlineStartwindow().getComPort())) {
JOptionPane.showMessageDialog(null, "Successpully opened port",
"Online Dump", JOptionPane.INFORMATION_MESSAGE);
}
}
// OPENS SERAIL PORT
serialPort = (SerialPort) port.open("SimpleReadApp1111", 1000);
// OPENS SERIAL PORT INPUT STREAM TO READ DATA
try {
inputStream = serialPort.getInputStream();
} catch (IOException e) {
System.out.println("IO Exception");
}
// ADDS LISTNER TO SERIALPORT
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {
System.out.println("Tooo many Listener exception");
}
// EVENT GENERATED WHEN DATA WILL BE AVAILABELE ON SERIALPORT
// INPUTSTREAM
serialPort.notifyOnDataAvailable(true);
try {
// SETS SELECTED BAUDRATE
int BAUDRATE = Integer.parseInt((getOnlineStartwindow()
.getBaudRate()).trim());
// SETS SELECTED DATA BITS
int DATABITS = Integer.parseInt(getOnlineStartwindow()
.getDataBits().trim());
if (DATABITS == 8) {
DATABITS = SerialPort.DATABITS_8;
} else if (DATABITS == 7) {
DATABITS = SerialPort.DATABITS_7;
} else if (DATABITS == 6) {
DATABITS = SerialPort.DATABITS_6;
} else if (DATABITS == 5) {
DATABITS = SerialPort.DATABITS_5;
}
// SETS SELECTED STOPBITS
int STOPBITS = 0;
if (getOnlineStartwindow().getStopBits() == "1") {
STOPBITS = SerialPort.STOPBITS_1;
} else if (getOnlineStartwindow().getStopBits() == "1.5") {
STOPBITS = SerialPort.STOPBITS_1_5;
} else if (getOnlineStartwindow().getStopBits() == "2") {
STOPBITS = SerialPort.STOPBITS_2;
}
// SETS SELECTED PARITY
int PARITY = 0;
if (getOnlineStartwindow().getParity() == "NONE") {
PARITY = SerialPort.PARITY_NONE;
} else if (getOnlineStartwindow().getParity() == "EVEN") {
PARITY = SerialPort.PARITY_EVEN;
} else if (getOnlineStartwindow().getParity() == "ODD") {
PARITY = SerialPort.PARITY_ODD;
}
// SETS SELECTED FLOW CONTROL
int FLOWCONTROL = 0;
if (getOnlineStartwindow().getFlowControl() == "NONE") {
FLOWCONTROL = SerialPort.FLOWCONTROL_NONE;
} else if (getOnlineStartwindow().getFlowControl() == "XON/XOFF") {
FLOWCONTROL = SerialPort.FLOWCONTROL_XONXOFF_IN;
}
serialPort
.setSerialPortParams(BAUDRATE, DATABITS, STOPBITS, PARITY);
// no handshaking or other flow control
serialPort.setFlowControlMode(FLOWCONTROL);
} catch (UnsupportedCommOperationException e) {
System.out.println("UnSupported comm operation");
}
}
// *********this method will automaticaly calls when u get data on port and
// arranges packet from start frame to end frame *************//
public void serialEvent(SerialPortEvent event) {
// switch (event.getEventType()) {
//
// case SerialPortEvent.DATA_AVAILABLE:
if(event.getEventType()==SerialPortEvent.DATA_AVAILABLE){
//dataAvailabel = inputStream.available();
// READING DATA CHARECTER BY CHARECTER
while (newData != -1) {
try {
newData = inputStream.read();
if (newData == -1) {
break;
}
if (Constants.SF == (char) newData) {
index = 0;
// System.out.println("start frame");
}
tempIntArray[index] = newData;
if (Constants.EF == (char) newData) {
selectToDispalyAndWrite(tempIntArray);
// disp(tempIntArray);
}
index++;
} catch (IOException ex) {
System.err.println(ex);
// return;
}
}
// ///////////////// completes
}
}
// DISPLYS PACKET TO TEXT AREA AND CREATES .PSI FILE
public void selectToDispalyAndWrite(int[] readBufferArray) {
if (getOnlineStartwindow().getDump().isSelected()) {
packetFilename = Integer.toString(readBufferArray[1])
+ Integer.toString(readBufferArray[2])
+ Integer.toString(readBufferArray[3]);
try {
if (getOnlineStartwindow().getFileTypeSelection() == "text") {
displayAndWriteToTextFile(readBufferArray);
} else {
displayAndWriteToExcelFile(readBufferArray);
}
} catch (IOException e) {
e.printStackTrace();
}
} else {
printToTextArea(readBufferArray);
}
}
public void printToTextArea(int[] readBufferArray) {
int i = 0;
int portname = 0;
Float portval = 0.0f;
int len = 0;
i = 0;
writeloop: while (len != readBufferArray.length) {
// while ((char) readBufferArray[i] != Constants.EF) {
if ((char) readBufferArray[i] == Constants.SF) {
// WRITES DASH LINE TO TEXT AREA
getOnlineStartwindow()
.getTextArea()
.append(
"\r\n\r\n-----------------------------------------------------------------------\r\n");
getOnlineStartwindow().getTextArea().append(
"Time :" + readBufferArray[i + 4] + " Min "
+ readBufferArray[i + 5] + " Sec.");
getOnlineStartwindow()
.getTextArea()
.append(
"\r\n-----------------------------------------------------------------------\r\n");
}
if ((char) readBufferArray[i] == Constants.EF) {
for (Iterator<Integer> iterator = channelMap.keySet()
.iterator(); iterator.hasNext();) {
int key = iterator.next();
Float value = channelMap.get(key);
getOnlineStartwindow().getTextArea().append(
"Port_" + key + " " + value + "\r\n");
}
channelMap.clear();
break writeloop;
}
i++;
}
}
public void displayAndWriteToTextFile(int[] readBufferArray)
throws IOException {
int i = 0;
int portname = 0;
Float portval = 0.0f;
if (!(filename.equalsIgnoreCase(packetFilename))) {
filename = packetFilename;
if (buffOut != null && fileWriter != null) {
drawLine('*');
buffOut.close();
fileWriter.close();
}
// GET CURRENT DATE
Date date = new Date();
file = new File(outFolder + "\\" + SDF.format(date) + "-"
+ packetFilename + ".txt");
if (!file.exists()) {
fileWriter = new FileWriter(file, true);
buffOut = new BufferedWriter(fileWriter);
drawLine('*');
drawLine('*');
} else {
fileWriter = new FileWriter(file, true);
buffOut = new BufferedWriter(fileWriter);
}
}
// LOOP TO DISPLY ALL PORT NAME AND PRESSURE VALUES
int len = 0;
i = 0;
writeloop: while (len != readBufferArray.length) {
// while ((char) readBufferArray[i] != Constants.EF) {
if ((char) readBufferArray[i] == Constants.SF) {
// WRITES DASH LINE TO TEXT AREA
getOnlineStartwindow()
.getTextArea()
.append(
"\r\n\r\n-----------------------------------------------------------------------\r\n");
getOnlineStartwindow().getTextArea().append(
"Time :" + readBufferArray[i + 4] + " Min "
+ readBufferArray[i + 5] + " Sec.");
getOnlineStartwindow()
.getTextArea()
.append(
"\r\n-----------------------------------------------------------------------\r\n");
drawLine('-');
buffOut.write("TIME: " + readBufferArray[i + 4] + " Min "
+ readBufferArray[i + 5] + " Sec." + "\r\n\r\n");
}
if ((char) readBufferArray[i] == Constants.ST) {
portname = readBufferArray[i + 1];
portval = getFloatValue(i);
channelMap.put(portname, portval);
}
if ((char) readBufferArray[i] == Constants.EF) {
for (Iterator<Integer> iterator = channelMap.keySet()
.iterator(); iterator.hasNext();) {
int key = iterator.next();
Float value = channelMap.get(key);
getOnlineStartwindow().getTextArea().append(
"Port_" + key + " " + value + "\r\n");
}
channelMap.clear();
break writeloop;
}
i++;
}
}
}
Thanks in advance
Here an approach from a design standpoint.
Create a class type for the data. This will be a container (inherit the appropriate queue). And inside of this class as you set and get the data make sure you lock.
Create a new thread in main to start the serial port and read the messages. Pass the thread one object (your list class object). The serial port will chuck data into this list. This will be sets in the object.
You main thread (the window) will have a timer that fires and reads the data from queue. You may have to play with the timer some. You don't want it firing too often and eating all your processing time in the main window. However if it is too slow you may not be updating enough and getting messages out of the queue when they are available. This timer should get all of the data out the queue and display it.
The queue and the lock are the important part here. The queue will keep messages in order. The lock will make sure you don't try to write new data to your queue while you are in the middle of reading data.
I was under the impression that Java does not support serial port for Windows, it only does it for Solaris or Linux.
your class should implement the Runnable Interface. and create a thread that reads from the serial port in its run method.
The Main thread could do the writing part to the TextArea.
You may need to do some synchronization between the Main Thread and the Runnable so that writing to the TextArea/File only takes place after appropriate data has been read from the port.
Related
I'm having a problem where I read a textfile named "songs.txt" with Scanner in a function called loadFiles() which every line is:
Music ID # Song Name # Release Date
And with this I create a Song object, and then store said object in a ArrayList. After reading the file, I clone this ArrayList so I can return a ArrayList with the songs read and clear the first ArrayList to prevent the cases where for exemple:
(PS: I use the ArrayLists as global variables)
songs.txt has this structure:
1oYYd2gnWZYrt89EBXdFiO#Message In A Bottle#1979
7zxc7dmd82nd92nskDInds#Sweet Child of Mine#1980
And the loadFiles() is called 2 times, the ArrayList would have a size of 4 instead of 2 as it should be. So that's why after songs.txt is read I copy the arrayList and then clear the first ArrayList that way the ArrayList that's returned only has the size of 2.
This is my code:
package pt.ulusofona.aed.deisiRockstar2021;
import java.io.IOException;
import java.util.Scanner;
import java.io.*;
import java.util.ArrayList;
public class Main {
public static ArrayList < Song > teste6 = new ArrayList < > ();
public static ArrayList < Song > getSongsArray = new ArrayList < > ();
public static ArrayList < Artista > testeSongArtists = new ArrayList < > ();
public static ParseInfo parseInfoSongsTxT = new ParseInfo(0, 0);
public static ParseInfo parseInfoSongsArtistsTxT = new ParseInfo(0, 0);
public static ParseInfo parseInfoSongsDetailsTxT = new ParseInfo(0, 0);
public static void main(String[] args) throws IOException {
ArrayList < Song > teste7 = new ArrayList < Song > ();
loadFiles();
loadFiles();
teste7 = getSongs();
ParseInfo teste8 = getParseInfo("songs.txt");
System.out.println("\n----------------------TESTE DO MAIN----------------------");
System.out.println(teste7.toString());
System.out.println(teste8.toString());
System.out.println(getSongsArray.size());
}
public static void loadFiles() throws IOException {
//Aqui lê-se o ficheiro songs.txt
System.out.println("----------------------LEITURA DO FICHEIRO songs.txt------------");
String nomeFicheiro = "songs.txt";
try {
File ficheiro = new File(nomeFicheiro);
FileInputStream fis = new FileInputStream(ficheiro);
Scanner leitorFicheiro = new Scanner(fis);
while (leitorFicheiro.hasNextLine()) {
String linha = leitorFicheiro.nextLine();
String dados[] = linha.split("#");
if (dados.length != 3) {
parseInfoSongsTxT.NUM_LINHAS_IGNORED += 1;
continue;
}
if (Character.isWhitespace(dados[0].charAt(0))) {
parseInfoSongsTxT.NUM_LINHAS_IGNORED += 1;
continue;
}
if (Character.isWhitespace(dados[1].charAt(0))) {
parseInfoSongsTxT.NUM_LINHAS_IGNORED += 1;
continue;
}
if (Character.isWhitespace(dados[2].charAt(0))) {
parseInfoSongsTxT.NUM_LINHAS_IGNORED += 1;
continue;
}
//Meter para ignorar a acabar com espaço
parseInfoSongsTxT.NUM_LINHAS_OK += 1;
String idTemaMusical = dados[0];
String nome = dados[1];
int anoLancamento = Integer.parseInt(dados[2]);
Song song = new Song(idTemaMusical, nome, null, anoLancamento, 0, false, 0, 0, 0, 0);
teste6.add(song);
}
leitorFicheiro.close();
getSongsArray = (ArrayList < Song > ) teste6.clone();
teste6.clear();
} catch (FileNotFoundException exception) {
String mensagem = "Erro: o ficheiro " + nomeFicheiro + " nao foi encontrado.";
System.out.println(mensagem);
}
System.out.println(teste6.toString());
System.out.println("Ok: " + parseInfoSongsTxT.NUM_LINHAS_OK + ", Ignored: " + parseInfoSongsTxT.NUM_LINHAS_IGNORED + "\n");
System.out.println("----------------------LEITURA DO FICHEIRO song_artists.txt------------");
//Aqui é lido o ficheiro song_artists.txt, mas falta ver se é preciso separar vários artistas com o mesmo ID para posições diferentes no ArrayList
String nomeFicheiro2 = "song_artists.txt";
try {
File song_artists = new File(nomeFicheiro2);
FileInputStream fis2 = new FileInputStream(song_artists);
Scanner leitorFicheiro2 = new Scanner(fis2);
while (leitorFicheiro2.hasNextLine()) {
String linha = leitorFicheiro2.nextLine();
String dados[] = linha.split("#");
if (dados.length != 2) {
parseInfoSongsArtistsTxT.NUM_LINHAS_IGNORED += 1;
continue;
}
if (Character.isWhitespace(dados[0].charAt(0))) {
parseInfoSongsTxT.NUM_LINHAS_IGNORED += 1;
continue;
}
if (Character.isWhitespace(dados[1].charAt(0))) {
parseInfoSongsTxT.NUM_LINHAS_IGNORED += 1;
continue;
}
parseInfoSongsArtistsTxT.NUM_LINHAS_OK += 1;
String idTemaMusical = dados[0];
String artista = dados[1];
Artista artista2 = new Artista(idTemaMusical, artista);
testeSongArtists.add(artista2);
}
leitorFicheiro2.close();
} catch (FileNotFoundException exception) {
String mensagem = "Erro: o ficheiro " + nomeFicheiro2 + " não foi encontrado.";
System.out.println(mensagem);
}
System.out.println(testeSongArtists.toString());
System.out.println("Ok: " + parseInfoSongsArtistsTxT.NUM_LINHAS_OK + ", Ignored: " + parseInfoSongsArtistsTxT.NUM_LINHAS_IGNORED + "\n");
System.out.println("----------------------LEITURA DO FICHEIRO song_details.txt------------");
//Aqui lê-se o ficheiro song_details.txt
boolean letra = false;
ArrayList < Song > testeSongDetails = new ArrayList < Song > ();
String nomeFicheiro3 = "song_details.txt";
try {
File song_details = new File(nomeFicheiro3);
FileInputStream fis3 = new FileInputStream(song_details);
Scanner leitorFicheiro3 = new Scanner(fis3);
while (leitorFicheiro3.hasNextLine()) {
String linha = leitorFicheiro3.nextLine();
String dados[] = linha.split("#");
if (dados.length != 7) {
parseInfoSongsDetailsTxT.NUM_LINHAS_IGNORED += 1;
continue;
}
if (Character.isWhitespace(dados[0].charAt(0))) {
parseInfoSongsTxT.NUM_LINHAS_IGNORED += 1;
continue;
}
if (Character.isWhitespace(dados[1].charAt(0))) {
parseInfoSongsTxT.NUM_LINHAS_IGNORED += 1;
continue;
}
if (Character.isWhitespace(dados[3].charAt(0))) {
parseInfoSongsTxT.NUM_LINHAS_IGNORED += 1;
continue;
}
if (Character.isWhitespace(dados[4].charAt(0))) {
parseInfoSongsTxT.NUM_LINHAS_IGNORED += 1;
continue;
}
if (Character.isWhitespace(dados[5].charAt(0))) {
parseInfoSongsTxT.NUM_LINHAS_IGNORED += 1;
continue;
}
if (Character.isWhitespace(dados[6].charAt(0))) {
parseInfoSongsTxT.NUM_LINHAS_IGNORED += 1;
continue;
}
parseInfoSongsDetailsTxT.NUM_LINHAS_OK += 1;
String idTemaMusical = dados[0];
//System.out.println(idTemaMusical);
int duracao = Integer.parseInt(dados[1]);
//System.out.println(duracao);
int letraExplicita = Integer.parseInt(dados[2]);
//System.out.println(letraExplicita);
if (letraExplicita == 0) {
letra = false;
} else {
letra = true;
}
//System.out.println(letra);
int populariedade = Integer.parseInt(dados[3]);
//System.out.println(populariedade);
double dancabilidade = Double.parseDouble(dados[4]);
//System.out.println(dancabilidade);
double vivacidade = Double.parseDouble(dados[5]);
//System.out.println(vivacidade);
double volumeMedio = Double.parseDouble(dados[6]);
//System.out.println(volumeMedio);
Song song = new Song(idTemaMusical, null, null, 0, duracao, letra, populariedade, dancabilidade, vivacidade, volumeMedio);
testeSongDetails.add(song);
}
leitorFicheiro3.close();
} catch (FileNotFoundException exception) {
String mensagem = "Erro: o ficheiro " + nomeFicheiro3 + " não foi encontrado.";
System.out.println(mensagem);
}
System.out.println("Ok: " + parseInfoSongsDetailsTxT.NUM_LINHAS_OK + ", Ignored: " + parseInfoSongsDetailsTxT.NUM_LINHAS_IGNORED);
}
public static ArrayList < Song > getSongs() {
return getSongsArray;
}
public static ParseInfo getParseInfo(String fileName) {
if (fileName == "songs.txt") {
return parseInfoSongsTxT;
}
if (fileName == "song_artists.txt") {
return parseInfoSongsArtistsTxT;
}
if (fileName == "song_details.txt") {
return parseInfoSongsDetailsTxT;
}
return null;
}
}
The problem is that when I made a test to check the function where the ArrayList is returned to see the size of the ArrayList it always comes as 0.
I think it's because only the function the returns the ArrayList is tested so loadFiles() isn't executed so the ArrayListo never gets cloned and that makes the ArrayList that is returned stay the same.
I thought about calling loadFiles() inside getSongs() and that way I would guarantee that the ArrayList is cloned but that would make getSongs use "throws IOException" and since I have to respect the school's project guide and getSongs doesn't include "throws IOException" i can't put it there.
But the more I think about it, that doesn't even make sense because how can they test it with a file of their own and loadFiles() isn't executed?
I'm out of ideas how to solve this problem, any help is welcome thank you.
I have to pass record to an UDF which calls an API but as we want to do it parallely,we are using spark and thats why UDF is being developed, the problem here is that that UDF needs to take only 100 records at a time not more than that, it can't handle more than 100 records parallely, so how to ensure that only 100 record pass to it in one go please note we don't want to use count() function on whole record.
I am attaching the UDF code here,it's a generic UDF which returns array of struct.moreover if we pass 100 records in batchsize variable each time then,if suppose there are 198 records then if as we dont want to use count() we will not be knowing that its last batchsize is going to be 98.so how to handle that thing.
Guys... I have a generic UDF in which call is made for an API but before calling it creates batch of 100 firstly then only call restapi.. So the argument UDF takes are x1:string, x2:string, batchsize:integer(currently the batchsize is 100)..so in UDF until and unless the batchsize is not 100 the call will not happen.. And for each record it will return null.
So till 99th record it will return. Null but at 100th record the call will happen
[So, now the problem part:as we are taking batchsize 100 and call will take place only at 100th record. So, in condition like if we have suppose 198 record in file then 100 record will get the output but, other 98 will only return null as they will not get processed..
So please help a way around, and UDF take one record at a time, but it keep on collecting till 100th record.. I hope this clears up
public class Standardize_Address extends GenericUDF {
private static final Logger logger = LoggerFactory.getLogger(Standardize_Address.class);
private int counter = 0;
Client client = null;
private Batch batch = new Batch();
public Standardize_Address() {
client = new ClientBuilder().withUrl("https://ss-staging-public.beringmedia.com/street-address").build();
}
// StringObjectInspector streeti;
PrimitiveObjectInspector streeti;
PrimitiveObjectInspector cityi;
PrimitiveObjectInspector zipi;
PrimitiveObjectInspector statei;
PrimitiveObjectInspector batchsizei;
private ArrayList ret;
#Override
public String getDisplayString(String[] argument) {
return "My display string";
}
#Override
public ObjectInspector initialize(ObjectInspector[] args) throws UDFArgumentException {
System.out.println("under initialize");
if (args[0] == null) {
throw new UDFArgumentTypeException(0, "NO Street is mentioned");
}
if (args[1] == null) {
throw new UDFArgumentTypeException(0, "No Zip is mentioned");
}
if (args[2] == null) {
throw new UDFArgumentTypeException(0, "No city is mentioned");
}
if (args[3] == null) {
throw new UDFArgumentTypeException(0, "No State is mentioned");
}
if (args[4] == null) {
throw new UDFArgumentTypeException(0, "No batch size is mentioned");
}
/// streeti =args[0];
streeti = (PrimitiveObjectInspector)args[0];
// this.streetvalue = (StringObjectInspector) streeti;
cityi = (PrimitiveObjectInspector)args[1];
zipi = (PrimitiveObjectInspector)args[2];
statei = (PrimitiveObjectInspector)args[3];
batchsizei = (PrimitiveObjectInspector)args[4];
ret = new ArrayList();
ArrayList structFieldNames = new ArrayList();
ArrayList structFieldObjectInspectors = new ArrayList();
structFieldNames.add("Street");
structFieldNames.add("city");
structFieldNames.add("zip");
structFieldNames.add("state");
structFieldObjectInspectors.add(PrimitiveObjectInspectorFactory.writableStringObjectInspector);
structFieldObjectInspectors.add(PrimitiveObjectInspectorFactory.writableStringObjectInspector);
structFieldObjectInspectors.add(PrimitiveObjectInspectorFactory.writableStringObjectInspector);
structFieldObjectInspectors.add(PrimitiveObjectInspectorFactory.writableStringObjectInspector);
StructObjectInspector si2 = ObjectInspectorFactory.getStandardStructObjectInspector(structFieldNames,
structFieldObjectInspectors);
ListObjectInspector li2;
li2 = ObjectInspectorFactory.getStandardListObjectInspector(si2);
return li2;
}
#Override
public Object evaluate(DeferredObject[] args) throws HiveException {
ret.clear();
System.out.println("under evaluate");
// String street1 = streetvalue.getPrimitiveJavaObject(args[0].get());
Object oin = args[4].get();
System.out.println("under typecasting");
int batchsize = (Integer) batchsizei.getPrimitiveJavaObject(oin);
System.out.println("batchsize");
Object oin1 = args[0].get();
String street1 = (String) streeti.getPrimitiveJavaObject(oin1);
Object oin2 = args[1].get();
String zip1 = (String) zipi.getPrimitiveJavaObject(oin2);
Object oin3 = args[2].get();
String city1 = (String) cityi.getPrimitiveJavaObject(oin3);
Object oin4 = args[3].get();
String state1 = (String) statei.getPrimitiveJavaObject(oin4);
logger.info("address passed, street=" + street1 + ",zip=" + zip1 + ",city=" + city1 + ",state=" + state1);
counter++;
try {
System.out.println("under try");
Lookup lookup = new Lookup();
lookup.setStreet(street1);
lookup.setCity(city1);
lookup.setState(state1);
lookup.setZipCode(zip1);
lookup.setMaxCandidates(1);
batch.add(lookup);
} catch (BatchFullException ex) {
logger.error(ex.getMessage(), ex);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
/* batch.add(lookup); */
if (counter == batchsize) {
System.out.println("under if");
try {
logger.info("batch input street " + batch.get(0).getStreet());
try {
client.send(batch);
} catch (Exception e) {
logger.error(e.getMessage(), e);
logger.warn("skipping current batch, continuing with the next batch");
batch.clear();
counter = 0;
return null;
}
Vector<Lookup> lookups = batch.getAllLookups();
for (int i = 0; i < batch.size(); i++) {
// ListObjectInspector candidates;
ArrayList<Candidate> candidates = lookups.get(i).getResult();
if (candidates.isEmpty()) {
logger.warn("Address " + i + " is invalid.\n");
continue;
}
logger.info("Address " + i + " is valid. (There is at least one candidate)");
for (Candidate candidate : candidates) {
final Components components = candidate.getComponents();
final Metadata metadata = candidate.getMetadata();
logger.info("\nCandidate " + candidate.getCandidateIndex() + ":");
logger.info("Delivery line 1: " + candidate.getDeliveryLine1());
logger.info("Last line: " + candidate.getLastLine());
logger.info("ZIP Code: " + components.getZipCode() + "-" + components.getPlus4Code());
logger.info("County: " + metadata.getCountyName());
logger.info("Latitude: " + metadata.getLatitude());
logger.info("Longitude: " + metadata.getLongitude());
}
Object[] e;
e = new Object[4];
e[0] = new Text(candidates.get(i).getComponents().getStreetName());
e[1] = new Text(candidates.get(i).getComponents().getCityName());
e[2] = new Text(candidates.get(i).getComponents().getZipCode());
e[3] = new Text(candidates.get(i).getComponents().getState());
ret.add(e);
}
counter = 0;
batch.clear();
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
return ret;
} else {
return null;
}
}
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am using the following code to download messages from Gmail.
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.FetchProfile;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.search.FlagTerm;
import org.openqa.selenium.WebDriver;
public class MailReader {
WebDriver driver;
Folder inbox;
String m;
String gmailID = "xyz#gmail.com";
String gmailPass = "xyz";
String storeMessage;
public MailReader()
{
}
public String readMail() {
System.out.println("Inside readMail()...");
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
/* Set the mail properties */
Properties props = System.getProperties();
// Set manual Properties
props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.pop3.socketFactory.fallback", "false");
props.setProperty("mail.pop3.port", "995");
props.setProperty("mail.pop3.socketFactory.port", "995");
props.put("mail.pop3.host", "pop.gmail.com");
try
{
/* Create the session and get the store for read the mail. */
Session session = Session.getDefaultInstance(
System.getProperties(), null);
Store store = session.getStore("pop3");
store.connect("pop.gmail.com", 995, gmailID,
gmailPass);
/* Mention the folder name which you want to read. */
// inbox = store.getDefaultFolder();
// inbox = inbox.getFolder("INBOX");
inbox = store.getFolder("INBOX");
/* Open the inbox using store. */
inbox.open(Folder.READ_ONLY);
/* Get the messages which is unread in the Inbox */
Message messages[] = inbox.search(new FlagTerm(new Flags(
Flags.Flag.SEEN), false));
System.out.println("No. of Unread Messages : " + messages.length);
/* Use a suitable FetchProfile */
FetchProfile fp = new FetchProfile();
fp.add(FetchProfile.Item.ENVELOPE);
fp.add(FetchProfile.Item.CONTENT_INFO);
inbox.fetch(messages, fp);
try
{
m = printAllMessages(messages);
inbox.close(true);
store.close();
}
catch (Exception ex)
{
System.out.println("Exception arise at the time of read mail");
ex.printStackTrace();
}
}
catch (MessagingException e)
{
System.out.println("Exception while connecting to server: "
+ e.getLocalizedMessage());
e.printStackTrace();
System.exit(2);
}
return m;
}
public String printAllMessages(Message[] msgs) throws Exception
{
String s = null;
for (int i = 0; i < msgs.length; i++)
{
//System.out.println("MESSAGE #" + (i + 1) + ":");
s = printEnvelope(msgs[i]);
}
return s;
}
public String printEnvelope(Message message) throws Exception
{
Address[] a;
// FROM
if ((a = message.getFrom()) != null) {
for (int j = 0; j < a.length; j++) {
System.out.println("FROM: " + a[j].toString());
}
}
// TO
if ((a = message.getRecipients(Message.RecipientType.TO)) != null) {
for (int j = 0; j < a.length; j++) {
System.out.println("TO: " + a[j].toString());
}
}
String subject = message.getSubject();
Date receivedDate = message.getReceivedDate();
Date sentDate = message.getSentDate(); // receivedDate is returning
// null. So used getSentDate()
String content = message.getContent().toString();
System.out.println("Subject : " + subject);
if (receivedDate != null) {
System.out.println("Received Date : " + receivedDate.toString());
}
System.out.println("Sent Date : " + sentDate.toString());
System.out.println("Content : " + content);
return(getContent(message));
}
public String getContent(Message msg)
{
try {
String contentType = msg.getContentType();
System.out.println("Content Type : " + contentType);
Multipart mp = (Multipart) msg.getContent();
BodyPart bp = mp.getBodyPart(0);
int count = mp.getCount();
for (int i = 0; i < count; i++) {
String s = getText(mp.getBodyPart(i));
if(i == 1) {
return s;
}
//dumpPart(mp.getBodyPar((i));
}
} catch (Exception ex) {
System.out.println("Exception arise at get Content");
ex.printStackTrace();
}
return m;
}
/*
public void dumpPart(Part p) throws Exception {
// Dump input stream ..
InputStream is = p.getInputStream();
// If "is" is not already buffered, wrap a BufferedInputStream
// around it.
if (!(is instanceof BufferedInputStream)) {
is = new BufferedInputStream(is);
}
int c;
System.out.println("Message : ");
while ((c = is.read()) != -1) {
System.out.write(c);
}
}*/
boolean textIsHtml = false;
/**
* Return the primary text content of the message.
*/
public String getText(Part p) throws
MessagingException, IOException {
if (p.isMimeType("text/*")) {
String s = (String)p.getContent();
textIsHtml = p.isMimeType("text/html");
return s;
}
if (p.isMimeType("multipart/alternative")) {
// prefer html text over plain text
Multipart mp = (Multipart)p.getContent();
String text = null;
for (int i = 0; i < mp.getCount(); i++) {
Part bp = mp.getBodyPart(i);
if (bp.isMimeType("text/plain")) {
if (text == null)
text = getText(bp);
continue;
} else if (bp.isMimeType("text/html")) {
String s = getText(bp);
if (s != null)
return s;
} else {
return getText(bp);
}
}
return text;
} else if (p.isMimeType("multipart/*")) {
Multipart mp = (Multipart)p.getContent();
for (int i = 0; i < mp.getCount(); i++) {
String s = getText(mp.getBodyPart(i));
if (s != null)
return s;
}
}
return null;
}
}
I want to modify the code so that:
It only downloads the most recent 10 unread messages
It only downloads messages which are sent from a specific address (For example, only messages sent from myemail#example.com
How can this be done?
Thanks!
The 10 most recent messages are the last 10 messages in your INBOX.
But some of them could be read, and some of them could be deleted. To find the 10 most recent unread messages you'll need to use a FlagTerm to search for messages where the SEEN flag is false. You might want to use an AndTerm to find messages where the DELETED flag is also false.
Note that Folder.search doesn't download any of the messages, it just tells you which messages match. You can then look at the last 10 of those messages and do whatever you need to do to "download" them.
Hopefully that's enough of a hint to get you started. If you still can't get it to work, show us what code you're using and what results you're getting.
I've been working with Kafka for two months, and I used this code to consume messages locally. I recently decided to distribute Zookeeper and Kafka and everything seems to work just fine. My issue started when I tried to use the consumer's code from a remote IP; Once I change seeds.add("127.0.0.1"); to seeds.add("104.131.40.xxx"); I get this error message:
run:
Error communicating with Broker [104.131.40.xxx] to find Leader for [temperature, 0] Reason:
java.net.ConnectException: Connection refused Can't find metadata for Topic and Partition. Exiting
BUILD SUCCESSFUL (total time: 21 seconds)r code here
this is the code that I currently use:
/*
Kafka API consumer reads 10 readings from the "temperature" topic
*/
package simpleexample;
import kafka.api.FetchRequest;
import kafka.api.FetchRequestBuilder;
import kafka.api.PartitionOffsetRequestInfo;
import kafka.common.ErrorMapping;
import kafka.common.TopicAndPartition;
import kafka.javaapi.*;
import kafka.javaapi.consumer.SimpleConsumer;
import kafka.message.MessageAndOffset;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SimpleExample {
public static void main(String args[]) {
SimpleExample example = new SimpleExample();
//long maxReads = Long.parseLong(args[0]);
long maxReads = 10;
//String topic = args[1];
String topic = "temperature";
//int partition = Integer.parseInt(args[2]);
int partition =0;
List<String> seeds = new ArrayList<String>();
//seeds.add(args[3]);
seeds.add("104.131.40.xxx");
//int port = Integer.parseInt(args[4]);
int port =9092;
try {
example.run(maxReads, topic, partition, seeds, port);
} catch (Exception e) {
System.out.println("Oops:" + e);
e.printStackTrace();
}
}
private List<String> m_replicaBrokers = new ArrayList<String>();
public SimpleExample() {
m_replicaBrokers = new ArrayList<String>();
}
public void run(long a_maxReads, String a_topic, int a_partition, List<String> a_seedBrokers, int a_port) throws Exception {
// find the meta data about the topic and partition we are interested in
//
PartitionMetadata metadata = findLeader(a_seedBrokers, a_port, a_topic, a_partition);
if (metadata == null) {
System.out.println("Can't find metadata for Topic and Partition. Exiting");
return;
}
if (metadata.leader() == null) {
System.out.println("Can't find Leader for Topic and Partition. Exiting");
return;
}
String leadBroker = metadata.leader().host();
String clientName = "Client_" + a_topic + "_" + a_partition;
SimpleConsumer consumer = new SimpleConsumer(leadBroker, a_port, 100000, 64 * 1024, clientName);
long readOffset = getLastOffset(consumer,a_topic, a_partition, kafka.api.OffsetRequest.EarliestTime(), clientName);
int numErrors = 0;
while (a_maxReads > 0) {
if (consumer == null) {
consumer = new SimpleConsumer(leadBroker, a_port, 100000, 64 * 1024, clientName);
}
FetchRequest req = new FetchRequestBuilder()
.clientId(clientName)
.addFetch(a_topic, a_partition, readOffset, 100000) // Note: this fetchSize of 100000 might need to be increased if large batches are written to Kafka
.build();
FetchResponse fetchResponse = consumer.fetch(req);
if (fetchResponse.hasError()) {
numErrors++;
// Something went wrong!
short code = fetchResponse.errorCode(a_topic, a_partition);
System.out.println("Error fetching data from the Broker:" + leadBroker + " Reason: " + code);
if (numErrors > 5) break;
if (code == ErrorMapping.OffsetOutOfRangeCode()) {
// We asked for an invalid offset. For simple case ask for the last element to reset
readOffset = getLastOffset(consumer,a_topic, a_partition, kafka.api.OffsetRequest.LatestTime(), clientName);
continue;
}
consumer.close();
consumer = null;
leadBroker = findNewLeader(leadBroker, a_topic, a_partition, a_port);
continue;
}
numErrors = 0;
long numRead = 0;
for (MessageAndOffset messageAndOffset : fetchResponse.messageSet(a_topic, a_partition)) {
long currentOffset = messageAndOffset.offset();
if (currentOffset < readOffset) {
System.out.println("Found an old offset: " + currentOffset + " Expecting: " + readOffset);
continue;
}
readOffset = messageAndOffset.nextOffset();
ByteBuffer payload = messageAndOffset.message().payload();
byte[] bytes = new byte[payload.limit()];
payload.get(bytes);
System.out.println(String.valueOf(messageAndOffset.offset()) + ": " + new String(bytes, "UTF-8"));
numRead++;
a_maxReads--;
}
if (numRead == 0) {
try {
Thread.sleep(1000);
} catch (InterruptedException ie) {
}
}
}
if (consumer != null) consumer.close();
}
public static long getLastOffset(SimpleConsumer consumer, String topic, int partition,
long whichTime, String clientName) {
TopicAndPartition topicAndPartition = new TopicAndPartition(topic, partition);
Map<TopicAndPartition, PartitionOffsetRequestInfo> requestInfo = new HashMap<TopicAndPartition, PartitionOffsetRequestInfo>();
requestInfo.put(topicAndPartition, new PartitionOffsetRequestInfo(whichTime, 1));
kafka.javaapi.OffsetRequest request = new kafka.javaapi.OffsetRequest(
requestInfo, kafka.api.OffsetRequest.CurrentVersion(), clientName);
OffsetResponse response = consumer.getOffsetsBefore(request);
if (response.hasError()) {
System.out.println("Error fetching data Offset Data the Broker. Reason: " + response.errorCode(topic, partition) );
return 0;
}
long[] offsets = response.offsets(topic, partition);
return offsets[0];
}
private String findNewLeader(String a_oldLeader, String a_topic, int a_partition, int a_port) throws Exception {
for (int i = 0; i < 3; i++) {
boolean goToSleep = false;
PartitionMetadata metadata = findLeader(m_replicaBrokers, a_port, a_topic, a_partition);
if (metadata == null) {
goToSleep = true;
} else if (metadata.leader() == null) {
goToSleep = true;
} else if (a_oldLeader.equalsIgnoreCase(metadata.leader().host()) && i == 0) {
// first time through if the leader hasn't changed give ZooKeeper a second to recover
// second time, assume the broker did recover before failover, or it was a non-Broker issue
//
goToSleep = true;
} else {
return metadata.leader().host();
}
if (goToSleep) {
try {
Thread.sleep(1000);
} catch (InterruptedException ie) {
}
}
}
System.out.println("Unable to find new leader after Broker failure. Exiting");
throw new Exception("Unable to find new leader after Broker failure. Exiting");
}
private PartitionMetadata findLeader(List<String> a_seedBrokers, int a_port, String a_topic, int a_partition) {
PartitionMetadata returnMetaData = null;
loop:
for (String seed : a_seedBrokers) {
SimpleConsumer consumer = null;
try {
consumer = new SimpleConsumer(seed, a_port, 100000, 64 * 1024, "leaderLookup");
List<String> topics = Collections.singletonList(a_topic);
TopicMetadataRequest req = new TopicMetadataRequest(topics);
kafka.javaapi.TopicMetadataResponse resp = consumer.send(req);
List<TopicMetadata> metaData = resp.topicsMetadata();
for (TopicMetadata item : metaData) {
for (PartitionMetadata part : item.partitionsMetadata()) {
if (part.partitionId() == a_partition) {
returnMetaData = part;
break loop;
}
}
}
} catch (Exception e) {
System.out.println("Error communicating with Broker [" + seed + "] to find Leader for [" + a_topic
+ ", " + a_partition + "] Reason: " + e);
} finally {
if (consumer != null) consumer.close();
}
}
if (returnMetaData != null) {
m_replicaBrokers.clear();
for (kafka.cluster.Broker replica : returnMetaData.replicas()) {
m_replicaBrokers.add(replica.host());
}
}
return returnMetaData;
}
}
You need to set the advertised.host.name instead of host.name in the kafka server.properties configuration file.
Is there an easy way to discover a File's creation time with Java? The File class only has a method to get the "last modified" time. According to some resources I found on Google, the File class doesn't provide a getCreationTime() method because not all file systems support the idea of a creation time.
The only working solution I found involes shelling out the the command line and executing the "dir" command, which looks like it outputs the file's creation time. I guess this works, I only need to support Windows, but it seems very error prone to me.
Are there any third party libraries that provide the info I need?
Update: In the end, I don't think it's worth it for me to buy the third party library, but their API does seem pretty good so it's probably a good choice for anyone else that has this problem.
With the release of Java 7 there is a built-in way to do this:
Path path = Paths.get("path/to/file");
BasicFileAttributes attributes = Files.readAttributes(path, BasicFileAttributes.class);
FileTime creationTime = attributes.creationTime();
It is important to note that not all operating systems provide this information. I believe in those instances this returns the mtime which is the last modified time.
Windows does provide creation time.
I've wrote a small test class some days ago, wish it can help you:
// Get/Set windows file CreationTime/LastWriteTime/LastAccessTime
// Test with jna-3.2.7
// [http://maclife.net/wiki/index.php?title=Java_get_and_set_windows_system_file_creation_time_via_JNA_(Java_Native_Access)][1]
import java.io.*;
import java.nio.*;
import java.util.Date;
// Java Native Access library: jna.dev.java.net
import com.sun.jna.*;
import com.sun.jna.ptr.*;
import com.sun.jna.win32.*;
import com.sun.jna.platform.win32.*;
public class WindowsFileTime
{
public static final int GENERIC_READ = 0x80000000;
//public static final int GENERIC_WRITE = 0x40000000; // defined in com.sun.jna.platform.win32.WinNT
public static final int GENERIC_EXECUTE = 0x20000000;
public static final int GENERIC_ALL = 0x10000000;
// defined in com.sun.jna.platform.win32.WinNT
//public static final int CREATE_NEW = 1;
//public static final int CREATE_ALWAYS = 2;
//public static final int OPEN_EXISTING = 3;
//public static final int OPEN_ALWAYS = 4;
//public static final int TRUNCATE_EXISTING = 5;
public interface MoreKernel32 extends Kernel32
{
static final MoreKernel32 instance = (MoreKernel32)Native.loadLibrary ("kernel32", MoreKernel32.class, W32APIOptions.DEFAULT_OPTIONS);
boolean GetFileTime (WinNT.HANDLE hFile, WinBase.FILETIME lpCreationTime, WinBase.FILETIME lpLastAccessTime, WinBase.FILETIME lpLastWriteTime);
boolean SetFileTime (WinNT.HANDLE hFile, final WinBase.FILETIME lpCreationTime, final WinBase.FILETIME lpLastAccessTime, final WinBase.FILETIME lpLastWriteTime);
}
static MoreKernel32 win32 = MoreKernel32.instance;
//static Kernel32 _win32 = (Kernel32)win32;
static WinBase.FILETIME _creationTime = new WinBase.FILETIME ();
static WinBase.FILETIME _lastWriteTime = new WinBase.FILETIME ();
static WinBase.FILETIME _lastAccessTime = new WinBase.FILETIME ();
static boolean GetFileTime (String sFileName, Date creationTime, Date lastWriteTime, Date lastAccessTime)
{
WinNT.HANDLE hFile = OpenFile (sFileName, GENERIC_READ); // may be WinNT.GENERIC_READ in future jna version.
if (hFile == WinBase.INVALID_HANDLE_VALUE) return false;
boolean rc = win32.GetFileTime (hFile, _creationTime, _lastAccessTime, _lastWriteTime);
if (rc)
{
if (creationTime != null) creationTime.setTime (_creationTime.toLong());
if (lastAccessTime != null) lastAccessTime.setTime (_lastAccessTime.toLong());
if (lastWriteTime != null) lastWriteTime.setTime (_lastWriteTime.toLong());
}
else
{
int iLastError = win32.GetLastError();
System.out.print ("获取文件时间失败,错误码:" + iLastError + " " + GetWindowsSystemErrorMessage (iLastError));
}
win32.CloseHandle (hFile);
return rc;
}
static boolean SetFileTime (String sFileName, final Date creationTime, final Date lastWriteTime, final Date lastAccessTime)
{
WinNT.HANDLE hFile = OpenFile (sFileName, WinNT.GENERIC_WRITE);
if (hFile == WinBase.INVALID_HANDLE_VALUE) return false;
ConvertDateToFILETIME (creationTime, _creationTime);
ConvertDateToFILETIME (lastWriteTime, _lastWriteTime);
ConvertDateToFILETIME (lastAccessTime, _lastAccessTime);
//System.out.println ("creationTime: " + creationTime);
//System.out.println ("lastWriteTime: " + lastWriteTime);
//System.out.println ("lastAccessTime: " + lastAccessTime);
//System.out.println ("_creationTime: " + _creationTime);
//System.out.println ("_lastWriteTime: " + _lastWriteTime);
//System.out.println ("_lastAccessTime: " + _lastAccessTime);
boolean rc = win32.SetFileTime (hFile, creationTime==null?null:_creationTime, lastAccessTime==null?null:_lastAccessTime, lastWriteTime==null?null:_lastWriteTime);
if (! rc)
{
int iLastError = win32.GetLastError();
System.out.print ("设置文件时间失败,错误码:" + iLastError + " " + GetWindowsSystemErrorMessage (iLastError));
}
win32.CloseHandle (hFile);
return rc;
}
static void ConvertDateToFILETIME (Date date, WinBase.FILETIME ft)
{
if (ft != null)
{
long iFileTime = 0;
if (date != null)
{
iFileTime = WinBase.FILETIME.dateToFileTime (date);
ft.dwHighDateTime = (int)((iFileTime >> 32) & 0xFFFFFFFFL);
ft.dwLowDateTime = (int)(iFileTime & 0xFFFFFFFFL);
}
else
{
ft.dwHighDateTime = 0;
ft.dwLowDateTime = 0;
}
}
}
static WinNT.HANDLE OpenFile (String sFileName, int dwDesiredAccess)
{
WinNT.HANDLE hFile = win32.CreateFile (
sFileName,
dwDesiredAccess,
0,
null,
WinNT.OPEN_EXISTING,
0,
null
);
if (hFile == WinBase.INVALID_HANDLE_VALUE)
{
int iLastError = win32.GetLastError();
System.out.print (" 打开文件失败,错误码:" + iLastError + " " + GetWindowsSystemErrorMessage (iLastError));
}
return hFile;
}
static String GetWindowsSystemErrorMessage (int iError)
{
char[] buf = new char[255];
CharBuffer bb = CharBuffer.wrap (buf);
//bb.clear ();
//PointerByReference pMsgBuf = new PointerByReference ();
int iChar = win32.FormatMessage (
WinBase.FORMAT_MESSAGE_FROM_SYSTEM
//| WinBase.FORMAT_MESSAGE_IGNORE_INSERTS
//|WinBase.FORMAT_MESSAGE_ALLOCATE_BUFFER
,
null,
iError,
0x0804,
bb, buf.length,
//pMsgBuf, 0,
null
);
//for (int i=0; i<iChar; i++)
//{
// System.out.print (" ");
// System.out.print (String.format("%02X", buf[i]&0xFFFF));
//}
bb.limit (iChar);
//System.out.print (bb);
//System.out.print (pMsgBuf.getValue().getString(0));
//win32.LocalFree (pMsgBuf.getValue());
return bb.toString ();
}
public static void main (String[] args) throws Exception
{
if (args.length == 0)
{
System.out.println ("获取 Windows 的文件时间(创建时间、最后修改时间、最后访问时间)");
System.out.println ("用法:");
System.out.println (" java -cp .;..;jna.jar;platform.jar WindowsFileTime [文件名1] [文件名2]...");
return;
}
boolean rc;
java.sql.Timestamp ct = new java.sql.Timestamp(0);
java.sql.Timestamp wt = new java.sql.Timestamp(0);
java.sql.Timestamp at = new java.sql.Timestamp(0);
for (String sFileName : args)
{
System.out.println ("文件 " + sFileName);
rc = GetFileTime (sFileName, ct, wt, at);
if (rc)
{
System.out.println (" 创建时间:" + ct);
System.out.println (" 修改时间:" + wt);
System.out.println (" 访问时间:" + at);
}
else
{
//System.out.println ("GetFileTime 失败");
}
//wt.setTime (System.currentTimeMillis());
wt = java.sql.Timestamp.valueOf("2010-07-23 00:00:00");
rc = SetFileTime (sFileName, null, wt, null);
if (rc)
{
System.out.println ("SetFileTime (最后修改时间) 成功");
}
else
{
//System.out.println ("SetFileTime 失败");
}
}
}
}
I've been investigating this myself, but I need something that will work across Windows/*nix platforms.
One SO post includes some links to Posix JNI implementations.
JNA-POSIX
POSIX for Java
In particular, JNA-POSIX implements methods for getting file stats with implementations for Windows, BSD, Solaris, Linux and OSX.
All in all it looks very promising, so I'll be trying it out on my own project very soon.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class CreateDateInJava {
public static void main(String args[]) {
try {
// get runtime environment and execute child process
Runtime systemShell = Runtime.getRuntime();
BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter filename: ");
String fname = (String) br1.readLine();
Process output = systemShell.exec("cmd /c dir \"" + fname + "\" /tc");
System.out.println(output);
// open reader to get output from process
BufferedReader br = new BufferedReader(new InputStreamReader(output.getInputStream()));
String out = "";
String line = null;
int step = 1;
while ((line = br.readLine()) != null) {
if (step == 6) {
out = line;
}
step++;
}
// display process output
try {
out = out.replaceAll(" ", "");
System.out.println("CreationDate: " + out.substring(0, 10));
System.out.println("CreationTime: " + out.substring(10, 16) + "m");
} catch (StringIndexOutOfBoundsException se) {
System.out.println("File not found");
}
} catch (IOException ioe) {
System.err.println(ioe);
} catch (Throwable t) {
t.printStackTrace();
}
}
}
/**
D:\Foldername\Filename.Extension
Ex:
Enter Filename :
D:\Kamal\Test.txt
CreationDate: 02/14/2011
CreationTime: 12:59Pm
*/
The javaxt-core library includes a File class that can be used to retrieve file attributes, including the creation time. Example:
javaxt.io.File file = new javaxt.io.File("/temp/file.txt");
System.out.println("Created: " + file.getCreationTime());
System.out.println("Accessed: " + file.getLastAccessTime());
System.out.println("Modified: " + file.getLastModifiedTime());
Works with Java 1.5 and up.
I like the answer on jGuru that lists the option of using JNI to get the answer. This might prove to be faster than shelling out and you may encounter other situations such as this that need to be implemented specifically for windows.
Also, if you ever need to port to a different platform, then you can port your library as well and just have it return -1 for the answer to this question on *ix.
This is a basic example in Java, using BasicFileAttributes class:
Path path = Paths.get("C:\\Users\\jorgesys\\workspaceJava\\myfile.txt");
BasicFileAttributes attr;
try {
attr = Files.readAttributes(path, BasicFileAttributes.class);
System.out.println("File creation time: " + attr.creationTime());
} catch (IOException e) {
System.out.println("oops un error! " + e.getMessage());
}