홈>
이 코드는 Linux에서는 잘 작동하지만 Windows 7에서는 작동하지 않습니다. 파일 내용을 업데이트하려면 출력 파일을 클릭해야합니다. 트릭은 어디에 있습니까?
Windows 7 전문가 인 NetBeans IDE 8.0 RC1 (빌드 201402242200)을 NetBeans 8.0 패치 1.1, JDK 1.8 버전으로 업데이트했습니다
package watchfilethreadmod;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.file.Files;
import static java.nio.file.LinkOption.NOFOLLOW_LINKS;
import java.nio.file.Path;
import java.nio.file.Paths;
import static java.nio.file.StandardWatchEventKinds.*;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class WatchFileThreadMod {
static class WatchFile {
String fileName;
long lastFilePos;
RandomAccessFile file;
public WatchFile(String _fileName, RandomAccessFile _file) {
fileName = _fileName;
lastFilePos = 0;
file = _file;
}
}
public static void shutDownListener(Thread thread) {
Thread thr = thread;
if (thr != null) {
thr.interrupt();
}
}
private static class MyWatchQueueReader implements Runnable {
/**
* the watchService that is passed in from above
*/
private WatchService myWatcher;
public ArrayList<WatchFile> threadFileToWatch;
public String dirPath;
public MyWatchQueueReader(String _dirPath, WatchService myWatcher, ArrayList<WatchFile> _threadFileToWatch) {
this.myWatcher = myWatcher;
this.threadFileToWatch = _threadFileToWatch;
this.dirPath = _dirPath;
}
private void openFile(WatchFile obj) {
try {
System.out.println("Open file "+obj.fileName);
obj.file = new RandomAccessFile(dirPath + "/" + obj.fileName, "r");
} catch (FileNotFoundException e) {
obj.file = null;
System.out.println("filename " + obj.fileName + " non trovato");
}
obj.lastFilePos = 0;
}
private void process(WatchEvent evt) {
String thisLine;
ArrayList<WatchFile> auxList = threadFileToWatch;
for (WatchFile obj : auxList) {
if (obj.fileName.equals(evt.context().toString())) {
if (obj.file == null) {
openFile(obj);
}
try {
obj.file.seek(obj.lastFilePos);
} catch (IOException e) {
System.err.println("Seek error: " + e);
}
try {
thisLine = obj.file.readLine();
if ((thisLine == null)&&(evt.kind() == ENTRY_MODIFY)) {
System.out.printf("---> thisLine == null received %s event for file: %s\n",
evt.kind(), evt.context());
obj.file.close();
System.out.println("Close file "+obj.fileName);
openFile(obj);
thisLine = obj.file.readLine();
}
while (thisLine != null) { // while loop begins here
if (thisLine.length() > 0) {
if (thisLine.substring(thisLine.length() - 1).equals("*")) {
obj.lastFilePos = obj.file.getFilePointer();
System.out.println(obj.fileName + ": " + thisLine);
}
}
thisLine = obj.file.readLine();
} // end while
} // end try
catch (IOException e) {
System.err.println("Error: " + e);
}
}
}
}
/**
* In order to implement a file watcher, we loop forever ensuring
* requesting to take the next item from the file watchers queue.
*/
@Override
public void run() {
try {
// get the first event before looping
WatchKey key = myWatcher.take();
while (key != null) {
// we have a polled event, now we traverse it and
// receive all the states from it
for (WatchEvent event : key.pollEvents()) {
WatchEvent.Kind eventType = event.kind();
if (eventType == OVERFLOW) {
continue;
}
process(event);
}
key.reset();
key = myWatcher.take();
}
} catch (InterruptedException e) {
ArrayList<WatchFile> auxList = threadFileToWatch;
for (WatchFile obj : auxList) {
if (obj.file != null) {
try {
obj.file.close();
System.out.println("chiusura file " + obj.fileName);
} catch (IOException ex) {
System.out.println("errore in chiusura file");
Logger.getLogger(WatchFileThreadMod.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
//e.printStackTrace();
}
System.out.println("Stopping thread");
}
}
public static void main(String[] args) throws Exception {
// get the directory we want to watch, using the Paths singleton class
//Path toWatch = Paths.get(DIRECTORY_TO_WATCH);
ArrayList<WatchFile> fileToWatch = new ArrayList<>();
String filename;
RandomAccessFile file;
fileToWatch.add(new WatchFile("EURUSD.rlt", new RandomAccessFile(args[0] + "/EURUSD.rlt", "r")));
filename = "EURCHF2.rlt";
try {
file = new RandomAccessFile(args[0] + "/" + filename, "r");
} catch (FileNotFoundException e) {
file = null;
System.out.println("filename " + filename + " non trovato");
}
fileToWatch.add(new WatchFile(filename, file));
fileToWatch = fileToWatch;
Path toWatch = Paths.get(args[0]);
if (toWatch == null) {
throw new UnsupportedOperationException("Directory not found");
}
// Sanity check - Check if path is a folder
try {
Boolean isFolder = (Boolean) Files.getAttribute(toWatch,
"basic:isDirectory", NOFOLLOW_LINKS);
if (!isFolder) {
throw new IllegalArgumentException("Path: " + toWatch + " is not a folder");
}
} catch (IOException ioe) {
// Folder does not exists
ioe.printStackTrace();
}
// make a new watch service that we can register interest in
// directories and files with.
WatchService myWatcher = toWatch.getFileSystem().newWatchService();
// start the file watcher thread below
MyWatchQueueReader fileWatcher = new MyWatchQueueReader(args[0], myWatcher, fileToWatch);
Thread processingThread = new Thread(fileWatcher, "FileWatcher");
processingThread.start();
toWatch.register(myWatcher, ENTRY_CREATE, ENTRY_MODIFY);
}
}
수정 :요청에 따라 코드가 줄어 듭니다.
편집 2: 파일 경로
편집 3 :데이터를 작성하는 데 사용하는 메타 트레이더 코드
#property strict
int file_handle;
string InpFileName = _Symbol + ".rlt"; // File name
input string InpDirectoryName = "Data"; // Folder name
int OnInit()
{
ResetLastError();
file_handle = FileOpen(InpDirectoryName + "//" + InpFileName, FILE_SHARE_READ|FILE_WRITE|FILE_TXT|FILE_ANSI);
if(file_handle == INVALID_HANDLE) {
PrintFormat("Failed to open %s file, Error code = %d", InpFileName, GetLastError());
ExpertRemove();
}
return INIT_SUCCEEDED;
}
void OnTick()
{
// file_handle = FileOpen(InpDirectoryName + "//" + InpFileName, FILE_SHARE_READ|FILE_WRITE|FILE_TXT|FILE_ANSI);
// Datetime), Bid, Volume
// string s = FileRead()
string s = TimeToStr(TimeGMT()) + "|" + Bid + "|" + Volume[0];
FileWriteString(file_handle, s + "|*\r\n");
FileFlush(file_handle);
//FileClose(file_handle);
}
void OnDeinit(const int reason)
{
FileClose(file_handle);
}
편집 4 :문제를보다 잘 보여주기위한 스크린 캐스트 : 출력 파일을 클릭 할 때만 데이터 업데이트
시계 서비스가 업데이트되지 않습니다
- 답변 # 1
- 답변 # 2
파일 경로에 따옴표가 없을 수 있습니다.
관련 자료
- c - 왜 printf가 원하는대로 작동하지 않습니까?
- ubuntu20.04 - 밝기 제어가 작동하지 않습니다
- java - 두 번째 반복에서는 루프가 작동하지 않습니다
- Kubernetes에서 DNS가 제대로 작동하지 않습니다
- debian - type -p java는 initd 스크립트에서 작동하지 않습니다
- android - proguard-rulespro가 R8에서 작동하지 않는 것 같습니다
- expo - @ react-native-community/datetimepicker는 iOS 14에서 작동하지 않습니다
- d3.js - d3exit () remove ()가 예상대로 작동하지 않습니다
- mouse - 키보드가 작동하지 않는 이유는 무엇입니까?
- haskell - 내 함수가 무한 목록에서 작동하지 않는 이유는 무엇입니까?
- javascript - documentdocumentElementstylesetProperty ( '-some-color', 'green')가 IE에서 작동하지 않습니다
- javascript - $(document) ready (function ()이 작동하지 않습니다
- bash - heredoc이 sftp 암호에 대해 작동하지 않는 이유는 무엇입니까?
- 앱 저장 창에서 AppleScript가 작동하지 않습니다
- javascript - windowmatchmedia ("(최대 너비 - 1700 픽셀)");작동하지 않습니까?
- javascript - 내 코드가 forEach 메소드에서 작동하지 않습니다
- mac - 키보드가 작동하지 않거나 켜지지 않음
- prepared statement - Java의 Sybase IQ에 대해 PreparedStatement가 작동하지 않습니다
- c# - ClaimsPrincipal이 ASP Net 핵심 MVC 웹 앱에서 작동하지 않습니다
- Lua require가 작동하지 않지만 파일이 추적에 있습니다
관련 질문
- Java Gradle Eclipse -IDE에서 빌드하기 전에 표시되지만 빌드 후에는 표시되지 않는 UTF-8 기호
- Windows에서 실행 중인 Java 프로세스의 프로세스 ID를 찾는 방법은 무엇입니까? 그리고 어떻게 혼자 프로세스를 종료합니까?
- java : FileWriter를 만들 수 없습니다.
- "[오류] {생략} 모듈 jdk.compiler는 EclIpse에서 Maven을 사용하는 동안 이름이 지정되지 않은 모듈 @73ad993b에 대해 "com.sun.tools.javac.processing을 열지" 않습니다.
- windows : 전체 경로를 입력하지 않고 디렉토리에서 java .jar 파일을 어떻게 사용할 수 있습니까?
우선, 전제 : 나는
WatchService
의 미래 사용자를 위해이 질문에 주로 대답하고 있습니다. , (나와 같은)이 문제가 발생할 수 있습니다 (예 : 일부 시스템 이벤트는 발생 후 신호를 보냅니다).문제는 Java에서이 기능의 구현이 고유 한 것이므로 플랫폼에 따라 다릅니다 (https://docs.oracle.com/javase/7/docs/api/java/nio/ 참조) '플랫폼 종속성'섹션에있는 file/WatchService.html).
특히, Windows 7 (및 MacOSX가 실패한 경우)에서 구현은 폴링을 사용하여 파일 시스템에서 변경 사항을 검색하므로
WatchService
의 알림의 '유연성'에 의존 할 수 없습니다 . 알림은 결국 신호를 보내지 만 언제 발생하는지에 대한 보장은 없습니다. 이 문제에 대한 엄격한 해결책은 없지만 많은 시행 착오 끝에 나에게 맞는 것이 무엇인지 설명 할 수 있습니다.먼저 등록 된 파일 (예 : '감시 된 파일')에 쓸 때마다 콘텐츠를 플러시하고 파일의 '마지막 수정 된'속성을 업데이트하려고합니다. :
둘째, 코드에서 새로 고침을 '트리거'하려고 시도합니다 (좋은 코드는 아니지만이 경우 99 %의 시간 동안 작동한다는 사실에 만족합니다)
또는 (
두 경우 모두ENTRY_MODIFY
를 시청하는 경우)outputDir
의 특정 파일에 )sleep
WatchService
의 기본 메커니즘에 단순히 '시간을 준다' 2 초가 필요한 것보다 훨씬 많지만 트리거 할 수 있습니다.